Factorial Program in C: The factorial of a positive integer n, denoted by n!, is the product of all positive descending integers less than or equal to n: Syntax for factorial number is:
n! = n.(n - 1).(n - 2).(n - 3)....3.2.1.
For example, the factorial of 5 (denoted as 5!) is
5! = 5.4.3.2.1 = 120
The factorial of 0! is 1, according to the convention for an empty product.
The Factorial operation in many mathematical areas is used in permutations and combinations, algebra and mathematical analysis.
We’ll be covering the following topics in this tutorial:
Algorithm of factorial program in C
START
Step 1 → Enter the value of Fact.
Step 2 → From value fact upto 1 multiply each digit.
Step 4 → The final value is factorial Number.
STOP
Pseudocode of factorial program in C
procedure factorial(n)
FOR value = 1 to n
factorial = factorial * value
END FOR
DISPLAY factorial
end procedure
Factorial in C using a for loop
#include<stdio.h> int main() { int i, Digit, fact = 1; printf("Enter a number to calculate its factorial : "); scanf("%d", &Digit); for (i = 1; i <= Digit; i++) fact = fact * i; printf("Factorial of %d = %d\n", Digit, fact); return 0; }
Step by Step working of the above Program Code:
• First, the computer reads the number to find the factorial. • Then, using the loop, the 'i' value is multiplied by the 'fact' value. • The loop continues until the 'Digit' value. • Finally, the given number's factorial value is printed. Suppose the number the user enters is 5 • It assigns the value of Digit = 5 , i = 1 , fact = 1</span> • Then the loop continues till the condition of the for loop is true.</span>
1. i<=Digit (1<=5) , for loop condition is true. fact = fact * i (fact = 1*1) So fact = 1 i++ (i=i+1) So i = 2 2. i<=Digit (2<=5) , for loop condition is true. fact = fact * i (fact = 1*2) So fact = 2 i++ (i = i+1) So i = 3 3. i<=Digit (3<=5) , for loop condition is true. fact = fact * i (fact = 2*3) So fact = 6 i++ (i=i+1) So i=4 4. i<=Digit (4<=5) , for loop condition is true. fact = fact * i (fact = 6*4) So fact = 24 i++ (i=i+1) So i = 5 5. i<=Digit (5<=5) , for loop condition is true. fact = fact * i (fact = 24*5) So fact = 120 i++ (i=i+1) So i = 6 6. i<=Digit (6<=5) , for loop condition is true. It comes out of the for loop. Finally it prints as given below The Factorial of 5 is 120
Thus the program execution is completed.
Output:
Factorial program in C using recursion
#include<stdio.h> #include<conio.h> void main() { int Digit,factorial; printf("\n Enter the number: "); scanf("%d",&Digit); factorial = fact(Digit); printf("\n The factorial of the number %d is %d",Digit,factorial); } int fact(int n) { if(n==0 || n==1) return 1; else return(n * fact(n-1)); }
A function calls itself in recursion. The factorial function is called in the above program. It would help if you first expressed your solution in the recursive form to resolve a recursion problem.
Step by Step working of the above Program Code:
• Then, the factorial value is calculated using a recursive function and returns the factorial value to the main function.
• Finally, the given number’s factorial value is printed.
Let us assume that the number entered by the user is 5.
• It assigns the value of n = 5.
• Then, using a variable fact, the recursive function fact(n) is called.
1. n==0 || n==1 (5==0 || 5==1) if condition is false. So it goes to the else part. else return(n * fact(n-1)) So, (return(5 * fact(4))) 2. n==0 || n==1 (4==0 || 4==1) if condition is false. So it goes to the else part. else return(n * fact(n-1)) So, (return(4 * fact(3))) 3. n==0 || n==1 (3==0 || 3==1) if condition is false. So it goes to the else part. else return(n * fact(n-1)) So, (return(3 * fact(2))) 4. n==0 || n==1 (2==0 || 2==1) if condition is false. So it goes to the else part. else return(n * fact(n-1)) So, (return(2 * fact(1))) 5. n==0 || n==1 (1==0 || 1==1) if condition is true. Finally, it prints as given below. The Factorial of the number 5 is 120 Thus the program execution is completed.
Output: