C Program to print Factorial of a number
Write a C program to find the Factorial of a number
In this program we will find the factorial of a number where the number should be entered by the user. Factorial is sequence of a number whose multiply by all previous number.
Ex:- No is 5.
Factorial of a 5=120
Note:-Factorial of n number is 1*2*3*…n. You will learn to calculate the factorial of a number using for loop in this example.
Step 1.Read the number n
Step 2.Initialize the variable i, fact=1,n
Step 3.To take a user input for factorial of number
Step 4.We use for loop with the condition(i=1;i<=number)
Step 5.Than do fact=fact*i
Step 6.Print the variable of fact.
//initialize of variable
int i, number, fact = 1;
//to take user input.
printf("Enter a number to calculate its factorial\n");
//use this loop of following statement
for (i = 1; i<= number;i++)
fact = fact * i;
//display of factorial of a given number
printf("Factorial of a number %d is = %d\n", number, fact);
Enter a number to calculate its factorial 4
Factorial of a number 4 is 24