C Examples

WAP in C to Calculate Factorial using Pointer!

The Program in C to Calculate Factorial using Pointer is given below:

#include <stdio.h>

int factorial(int *n) {
    int result = 1;
    int temp = *n;
    while (*n > 0) {
        result *= *n;
        (*n)--;
    }
    *n = temp;
    return result;
}

int main() {
    int num;
    int *p = #

    printf("Enter a positive integer: ");
    scanf("%d", &num);

    int factorial_result = factorial(p);
    printf("Factorial of %d = %d\n", num, factorial_result);

    return 0;
}

Output:

Enter a positive integer: 10
Factorial of 10 = 3628800

Pro-Tips💡

A pointer 'p' is declared in the main function, and it is assigned the address of the variable ‘num'.

The pointer ‘p' is then passed as an argument to the 'factorial' function, where it is used to access and modify the value stored in 'num'.

The 'factorial' function uses the dereference operator '*' to access the value pointed to by 'p',

and it modifies the value stored at the address by decrementing it in each iteration.

Learn C-Sharp ↗

C-sharp covers every topic to learn about C-Sharp thoroughly.

Learn C Programming ↗

C-Programming covers every topic to learn about C-Sharp thoroughly.

Learn C++ Programming↗

C++ covers every topic to learn about C-Sharp thoroughly.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

Related Posts

C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns of the matrix…

C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns…

C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns in the matrix:22Enter…

C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below: Output: Hello Codeauri Family, enter the number of rows/columns in the matrices:2Enter the elements…

C# Program to Delete Element at Desired position From Array

The Program in C# Program to Delete Element at Desired position From Array is given below: Output: Hello Codeauri Family, enter the number of elements in the array:4Enter…

Leave a Reply

Your email address will not be published. Required fields are marked *

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X