How to write a C Program to find the Factorial of a Number using For Loop, While Loop, Pointers, Functions, Call by Reference, and Recursion. It is 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 finds the Factorial of a number using For Loop.
#include <stdio.h>int main(void){ int i, n; long fac = 1; printf("Please Enter any number to Find Factorial = "); scanf("%d", &n); for (i = 1; i <= n; i++) { fac = fac * i; } printf("\nFactorial of %d = %ld\n", n, fac); return 0;}
In this C Program to find the Factorial of a Number example, we declared two integer variables i and number. We also declared one long variable and assigned a value of 1.
Although we calculate for an integer variable, we declare the 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 the (i <= Number) condition will help the loop terminate when the condition fails.
The user entered an integer in the above example to find the factorial number program 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 the Number, and the second %d refers to the output.
printf("Factorial of %d = %d\n", Number, Factorial);
C Program to find the Factorial of a Number Using While loop
This C program to find the factorial of a number using a while loop allows you to enter any integer value. By using the value, this will find and return the result.
#include <stdio.h> int main(){ int Num, i = 1; long Fc = 1; printf("\n Please Enter any value \n"); scanf("%d", &Num); while (i <= Num) { Fc = Fc * i; i++; } printf("Factorial of %d = %d\n", Num, Fc); return 0;}
We 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 C program will find the Factorial of a given number using pointers.
I suggest you refer to the Pointers article before this example. It will help you to understand the Pointers and Pointer variable concepts.
#include <stdio.h>int main(){ int i, Num, *P; long Fct = 1; printf("\n Please Enter any \n"); scanf("%d", & Num); P = &Num; for (i = 1; i <= *P; i++) { Fct = Fct * i; } printf("\nFact of %d Using Normal Variable = %d\n", Num, Fct); printf("Fact of %d Using Pointer Variable = %d\n", *P, Fct); return 0;}
In this program, 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 = &Num;
Within the For Loop, We checked i value against the pointer variable *P instead of the 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. The user entered value will be passed to the Function we created. Within this User defined function, this C program finds the Factorial of a number using For Loop.
#include <stdio.h>long CaclFtr(int); int main(){ int Nm; long Fc = 1; printf("\n Please Enter any number \n"); scanf("%d", &Nm); Fc = CaclFtr(Nm); printf("Factorial of %d = %d\n", Nm, Fc); return 0;}long CaclFtr(int Nm){ int i; long Fc = 1; for (i = 1; i <= Nm; i++) { Fc = Fc * i; } return Fc;}
In this factorial of a number example, Since the function CaclFtr() will return the long value as output, we assigned the function calling to the long variable.
Factorial = CaclFtr(Nc);
When the compiler reaches the CaclFtr(Nc) line in the main() function, the compiler will immediately jump to the below function:
long CaclFtr(int Nc)
We already explained the logic in the above program example.
The last line ends with a return Statement. It means every time we call the CaclFtr() function from the main () or any sub-functions, 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 finds the Factorial of a number Recursively.
Please Refer to the Recursion in C article before this example. It will help you to understand the recursion Concept.
#include <stdio.h>long CalFct(int); int main(){ int Num; long Fact = 1; printf("\n Please Enter any \n"); scanf("%d", &Num); Fact = CalFct(Num); printf("\nFactorial of %d = %d\n", Num, Fact); return 0;}long CalFct(int Num){ if (Num == 0 || Num == 1) return 1; else return Num * CalFct(Num -1);}
Within the user-defined function of this program,
long CalFct(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 recursively return Num * (Num -1).
In this factorial of a number example, user Entered Value = 9. It means the First If statement fails So,
Fact = Num * CalFct (Num -1);
= 9 * CalFct (9 -1)
= 9 * CalFct (8)
= 9 * 8 * CalFct (7)
= 9 * 8 * 7 * CalFct (6)
= 9 * 8 * 7 * 6 * CalFct (5)
= 9 * 8 * 7 * 6 * 5 * CalFct (4)
= 9 * 8 * 7 * 6 * 5 * 4 * CalFct (3)
= 9 * 8 * 7 * 6 * 5 * 4 * 3 * CalFct (2)
= 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * CalFct (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 finds the Factorial of a number using For Loop and call by reference.
Please refer to the Call By Value and Call By Reference articles before this program example. It will help you to understand the difference between call by value and call by reference concepts.
#include <stdio.h>long CalFct(int *); int main(){ int Num; long Fact = 1; printf("\n Please Enter any value \n"); scanf("%d", &Num); Fact = CalFct(&Num); printf("Factorial of %d = %d\n", Num, Fact); return 0;}long CalFct(int *Num){ int i; long Fact = 1; for (i = 1; i <= *Num; i++) { Fact = Fact * i; } return Fact;}
Please Enter any value 8Factorial of 8 = 40320