Factorial program in C
Here, you will know about factorial and get the example code to make factorial program in c using 6 different ways.
What is Factorial?
The product of all positive integers less than or equal to a specific number is a factorial. To find the Factorial of a number calculate the product of all the integers from 1 to that number or from number to 1(in reverse number order).
For example:-
Factorial of 6 is 1*2*3*4*5*6 = 720.
or
Factorial of 6 is 6*5*4*3*2*1 = 720.
What is the factorial of 100
Factorial of 100 will be :
= 100 * 99 * 98 * 97 * 96……………. * 3 * 2 * 1
=93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
or
= 9.332622e+157
Factorial Algorithm
Step 1: Initialize f =1
Step 2: Input the value of n( whose factorial is to be found).
Step 3: Initialize i =1,f=1.
Step 4: while i < =n, repeat step 5 and step 6
Step 5: The value of i should be multiplied by f and saved in f.
Step 6: Increment value of i with 1.
Step 7: Print the value of variable f as the factorial of given number.
Factorial Flowchart
Now we knows the Logic, Algorithm, and Flowchart of Factorial. Let’s implements it with coding using six different methods.
Factorial Program using 6 different ways
Factorial Program In C Using For Loop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | //Factorial Program In C Using For Loop //C program to find factorial of a given number #include<stdio.h> #include<conio.h> void main() { long int f=1; int n,i; printf(“\nEnter the number to find factorial:”); scanf(“%d”,&n); for(i=1;i<=n;i++) f=f*i; printf(“\nFactorial of %d is: %ld”,n,f); getch( ); } |
Output
Factorial Program In C Using While Loop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | //Factorial Program In C Using While Loop //C program to find factorial of a given number #include<stdio.h> #include<conio.h> void main() { long int f=1; int n,i=1; printf(“\nEnter the number to find factorial:”); scanf(“%d”,&n); while(i<=n) { f=f*i; i++; } printf(“\nFactorial of %d is: %ld”,n,f); getch( ); } |
Output
Factorial program in c using function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | //Factorial program in c using function #include <stdio.h> int Factorial(int); int Factorial(int Num) { int i; int Factorial = 1; for (i = Num; i >= 1; i--) { Factorial = Factorial * i; } return Factorial; } int main() { int Num; int Fact; printf("\nEnter the number to find factorial : "); scanf("%d", &Num); Fact = Factorial(Num); printf("\nFactorial of %d is %d", Num, Fact); return 0; } |
Output
Factorial Program in C Using Recursion
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | //Factorial Program in C Using Recursion #include<stdio.h> #include<conio.h> int factorial(int n) { if(n>1) return n*factorial(n-1); else return 1; } int main() { int n; printf("\nEnter the number to find factorial : "); scanf("%d",&n); printf("\nThe factorial of %d is %d",n,factorial(n)); return 0; } |
Output
Factorial Program Using Ternary Operator
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | //C Program to Find Factorial Using Ternary Operator #include<stdio.h> #include<conio.h> int factorial(int n) { return (n==1 || n==0) ? 1: n*factorial(n-1); } int main() { int n; printf("\nEnter the number to find factorial : "); scanf("%d",&n); printf("\nThe factorial of %d is %d",n,factorial(n)); return 0; } |
Output
Factorial Program Using Pointers in C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | //C Program to Find Factorial Using Pointers #include <stdio.h> int main() { int num, i, fact; int *numPtr = # int *factPtr = &fact; printf("\nEnter the number to find factorial : "); scanf("%d", numPtr); if(*numPtr < 0) { printf("\nFactorial of a negative number does not exist."); } else { *factPtr = 1; for(i=2; i<=*numPtr; ++i) { *factPtr *= i; } printf("\nFactorial of %d is %d", *numPtr, *factPtr); } return 0; } |
Output