### Factorial Program in C

It is denoted with the symbol (!). The Factorial of any number can be, product of all the numbers that 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

In this article, we will show you How to write a C Program to find Factorial of a Number using For Loop, While Loop, Pointers, Functions, Call by Reference and Recursion.

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

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

/* C Program to 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; }

**OUTPUT:**

**ANALYSIS:**

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

**NOTE:** Although we are calculating the C factorial of an integer variable we declared output (Factorial) as a long variable. Because, When we are calculating the factorial for large integers then the result will definitely cross the integer limit.

Below printf statement will ask the user to enter his/her own integer value to calculate the factorial

printf("\n Please Enter any number to Find Factorial\n");

The scaf statement will assign a user entered value to the Number variable

scanf("%d", & Number);

**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.

for (i = 1; i <= Number; i++) { Factorial = Factorial * i; }

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

**First Iteration**

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

Factorial = Factorial * i;

Factorial = 1 *1 = 1

i++ means i will become 2

**Second Iteration**

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

Factorial = Factorial * i;

Factorial = 1 *2 = 2

i++ means i will become 3

**Third Iteration**

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

Factorial = Factorial * i;

Factorial = 2 *3 = 6

i++ means I will become 4

**Fourth Iteration**

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

Factorial = Factorial * i;

Factorial = 6 * 4 = 24

i++ means i will become 5 – It means (i <= Number) is Flase. So, For loop will be Terminated.

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

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

Factorial of 4 = 24

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

This program for factorial program in c allows you to enter any integer value. Using this value, this C program find Factorial of a number using While Loop

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

**OUTPUT:**

**ANALYSIS:**

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

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

This program for factorial in c allows the user to enter any integer value. Using this value, this program will find Factorial of a given number using pointers.

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

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

**OUTPUT:**

**ANALYSIS:**

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 C factorial 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 find Factorial of a number using For Loop.

/* C Program to Find Factorial of a Number Using Functions */ #include <stdio.h> long Calculate_Factorial(int); int main() { int Number; long Factorial = 1; printf("\n Please Enter any number to Find Factorial\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; }

**OUTPUT**

**ANALYSIS**

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

Factorial = Calculate_Factorial(Number);

When the compiler reaches to Calculate_Factorial(Number) line in main() function of a factorial program in c then the compiler will immediately jump to below function:

long Calculate_Factorial(int Number)

We already explained LOGIC in the above example.

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

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

This factorial program in c 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 Recursion in C article before this example. It will help you to understand the recursion Concept.

/* C Program to Find Factorial of a Number Using Recursion */ #include <stdio.h> long Calculate_Factorial(int); int main() { int Number; long Factorial = 1; printf("\n Please Enter any number to Find Factorial\n"); scanf("%d", &Number); Factorial = Calculate_Factorial(Number); printf("\nFactorial of %d = %d\n", Number, Factorial); return 0; } long Calculate_Factorial(int Number) { if (Number == 0 || Number == 1) return 1; else return Number * Calculate_Factorial (Number -1); }

**OUTPUT:**

**ANALYSIS:**

Within the user-defined function of this factorial program in c,

long Calculate_Factorial(int Number)

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

- If the condition is TRUE then the function will return 1.
- and, if the condition is TRUE then 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 C factorial program allows the user to enter any integer value. Instead of User entered value, the address of the variable will be passed 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 in C article before this factorial program in C example. It will help you to understand the difference between call by value and call by reference Concept.

/* C Program to Find Factorial of a Number Using Call By Reference */ #include <stdio.h> long Calculate_Factorial(int *); int main() { int Number; long Factorial = 1; printf("\n Please Enter any number to Find Factorial\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; }

**OUTPUT:**

Thank you for Visiting Our Blog