How to write a C Program to find Factorial of a Number using For Loop, While Loop, Pointers, Functions, Call by Reference and Recursion. It denoted with the symbol (!). The Factorial is the product of all numbers, which are less than or equal to that number, and greater than 0.

n! = n * (n-1) * (n -2) * …….* 1

For example, Factorial of 5 is represented as 5! = 5 *4 * 3 * 2 * 1 = 120

## C Program to Find Factorial of a Number Using For Loop

This factorial program allows the user to enter any integer value. By using this value, this C program find Factorial of a number using For Loop.

#include <stdio.h> int main() { int i, Number; long Factorial = 1; printf("\n Please Enter any number to Find Factorial\n"); scanf("%d", &Number); for (i = 1; i <= Number; i++) { Factorial = Factorial * i; } printf("\nFactorial of %d = %d\n", Number, Factorial); return 0; }

In this factorial program in c, we declared two integer variables i and number. We also declared one long variable and assigned a value of 1.

Although we are calculating for an integer variable, we declared output as a long variable. Because when we are calculating the factorial for large integers, then the result will definitely cross the integer limit

The first C programming printf statement will ask the user to enter his/her own integer value to calculate. And the scanf statement will assign a user-entered value to the Number variable

Within the For Loop, We initialized the integer I value to 1 and also (i <= Number) condition will help the loop to terminate when the condition fails.

User entered an integer in the above example for the factorial program in c is 4

First Iteration

i = 1, Factorial= 1 and Number= 4 – It means (i <= Number) is True

Factorial = Factorial * i => 1 *1 = 1

i++ means i will become 2

Second Iteration

i = 2, Factorial= 1 and Number= 4 – It means (2 <= 4) is True

Factorial = 1 *2 = 2

i++ means i will become 3

C Program to find Factorial of a Number Third Iteration

i = 3, Factorial= 2 and Number= 4 – It means (3 <= 4) is True

Factorial= 2 *3 = 6

i++ means I will become 4

Fourth Iteration

i = 4, Factorial= 6 and Number= 4 – It means (4 <= 4) is True

Factorial = 6 * 4 = 24

i++ means i will become 5 – It means (5 <= 4) is False. So, For loop will terminate.

The last printf statement will print the output of the user entered integer. Within the printf statement, the first %d refers to Number, and the second %d refers to output.

printf("Factorial of %d = %d\n", Number, Factorial);

## C Program for Factorial of a Number Using While loop

This c program for factorial of a number using a while loop allows you to enter any integer value. By using the value, this will find Factorial of a Number

#include <stdio.h> int main() { int Number, i = 1; long Factorial = 1; printf("\n Please Enter any value \n"); scanf("%d", &Number); while (i <= Number) { Factorial = Factorial * i; i++; } printf("Factorial of %d = %d\n", Number, Factorial); return 0; }

```
Please Enter any value
6
Factorial of 6 = 720
```

We just replaced the For loop in the above example with the While loop. If you don’t understand the While Loop program, please refer While Loop article here: While Loop

## C Program to Find Factorial of a Number Using Pointers

This program in c will find Factorial of a given number using pointers.

I suggest you refer Pointers article before this example. It will help you to understand the Pointers and Pointer variable Concept.

#include <stdio.h> int main() { int i, Number, *P; long Factorial = 1; printf("\n Please Enter any number \n"); scanf("%d", & Number); P = &Number; for (i = 1; i <= *P; i++) { Factorial = Factorial * i; } printf("\nFact of %d Using Normal Variable = %d\n", Number, Factorial); printf("Fact of %d Using Pointer Variable = %d\n", *P, Factorial); return 0; }

```
Please Enter any number
7
Fact of 7 Using Normal Variable = 5040
Fact of 7 Using Pointer Variable = 5040
```

In this factorial program in c, we assigned the address of the Number variable to the address of the pointer variable. Here, P is the address of the pointer variable that we already declared (*P). As we all know, &Number is the address of the Number.

P = &Number;

Within the For Loop, We checked i value against the pointer variable *P instead of Number variable.

Here, *P means Value inside the pointer variable

## C Program to Find Factorial of a Number Using Functions

This program allows the user to enter any integer value. User entered value will be passed to the Function we created. Within this User defined function, this C program finds Factorial of a number using For Loop.

#include <stdio.h> long Calculate_Factorial(int); int main() { int Number; long Factorial = 1; printf("\n Please Enter any number \n"); scanf("%d", &Number); Factorial = Calculate_Factorial(Number); printf("Factorial of %d = %d\n", Number, Factorial); return 0; } long Calculate_Factorial(int Number) { int i; long Factorial = 1; for (i = 1; i <= Number; i++) { Factorial = Factorial * i; } return Factorial; }

```
Please Enter any number
10
Factorial of 10 = 3628800
```

In this c program to find factorial of a number example, Since the function Calculate_Factorial() will return the long value as output, we assigned the function calling to long variable.

Factorial = Calculate_Factorial(Number);

When the compiler reaches to Calculate_Factorial(Number) line in main() function, the compiler will immediately jump to below function:

long Calculate_Factorial(int Number)

We already explained the C Program to find Factorial of a Number logic in the above program example.

The last line ends with a return Statement. It means every time we call the Calculate_Factorial () function from the main () or any sub-functions, then it will return the value.

## C Program to Find Factorial of a Number Using Recursion

This program allows you to enter any integer value. User entered value will be passed to the Function we created. Within this User defined function, this C program find Factorial of a number Recursively.

Please Refer to Recursion in C article before this example. It will help you to understand the recursion Concept.

#include <stdio.h> long Calculate_Factorial(int); int main() { int Number; long Fact = 1; printf("\n Please Enter any number \n"); scanf("%d", &Number); Fact = Calculate_Factorial(Number); printf("\nFactorial of %d = %d\n", Number, Fact); return 0; } long Calculate_Factorial(int Num) { if (Num == 0 || Num == 1) return 1; else return Num * Calculate_Factorial (Num -1); }

```
Please Enter any number
9
Factorial of 9 = 362880
```

Within the user-defined function of this program,

long Calculate_Factorial(int Num)

If Statement will check whether the Given Number is Equal to 0 or Equal to 1

- If the condition is TRUE, the function will return 1.
- And, if the condition is TRUE, the function will return Number * (Number -1) recursively.

In this c program to find factorial of a number example, user Entered Value = 9. It means, First If statement fails So,

Factorial = Number * Calculate_Factorial (Number -1);

= 9 * Calculate_Factorial(9 -1)

= 9 * Calculate_Factorial(8)

= 9 * 8 * Calculate_Factorial(7)

= 9 * 8 * 7 * Calculate_Factorial (6)

= 9 * 8 * 7 * 6 * Calculate_Factorial (5)

= 9 * 8 * 7 * 6 * 5 * Calculate_Factorial (4)

= 9 * 8 * 7 * 6 * 5 * 4 * Calculate_Factorial (3)

= 9 * 8 * 7 * 6 * 5 * 4 * 3 * Calculate_Factorial (2)

= 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * Calculate_Factorial (1)

= 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2

= 362880

## C Program to Find Factorial of a Number Using Call By Reference

This allows the user to enter any integer value. Instead of User entered value, the address of the variable will pass to the Function we created. Within this User defined function, this c program find Factorial of a number using For Loop.

Please Refer Call By Value and Call By Reference article before this program example. It will help you to understand the difference between call by value and call by reference Concept.

#include <stdio.h> long CalculateFactorial(int *); int main() { int Num; long Fact = 1; printf("\n Please Enter any value \n"); scanf("%d", &Num); Fact = Calculate_Factorial(&Num); printf("Factorial of %d = %d\n", Num, Fact); return 0; } long CalculateFactorial(int *Num) { int i; long Fact = 1; for (i = 1; i <= *Num; i++) { Fact = Fact * i; } return Fact; }

```
Please Enter any value
8
Factorial of 8 = 40320
```