Problem: Program to find factorial of given number.
Process: To find the factorial of given number we can repeatedly multiply the numbers fron 1 to N and store the result in a variable.
- Initialize a variable, factorial, to 1.
- Iterate a loop from 1 to N, multiplying the loop variable to factorial in each iteration.
#include<stdio.h>
int main() {
int num, i;
long int factorial = 1;
scanf("%d",&num);
for(int i=1;i<=num;i++){
factorial *= i;
}
printf("%ld",factorial);
return 0;
}
int main() {
int num, i;
long int factorial = 1;
scanf("%d",&num);
for(int i=1;i<=num;i++){
factorial *= i;
}
printf("%ld",factorial);
return 0;
}
Sample Input
5
Sample Output
120
5
Sample Output
120