# Algorithm to find factorial of a number using iteration in C program

29-01-2023

Here is the Algorithm to find factorial of a number using iteration and with examples in C program.

### Factorial of a Number using Iteration

Algorithm

1. Initialize a variable “num” to store the input number, a variable “i” to use as a counter in the loop, and a variable “fact” to store the factorial result, initially set to 1.
2. Prompt the user to enter the number for which the factorial is to be calculated.
3. Use a for loop to iterate from 1 to “num”.
4. On each iteration, multiply the current value of “fact” by the current value of “i” and store the result in “fact”.
5. At the end of the loop, the value of “fact” will be the factorial of the input number.
6. Print the factorial of the input number to the console.
7. Exit the program.

Program Code

``````#include <stdio.h>
int main() {
int num, i;
long long int fact = 1;
printf("Enter a number: ");
scanf("%d", &num);
for (i = 1; i <= num; i++) {
fact = fact * i;
}
printf("Factorial of %d is %lld", num, fact);
return 0;
}``````

This program prompts the user to enter a number, then uses a for loop to iterate from 1 to that number. On each iteration, the variable “fact” is multiplied by the current value of “i” in order to calculate the factorial of the input number. The final result is then printed to the console.