include
C program to find the factorial of a number first declares three integer variables – num, fact and i.
It prompts the user to enter a number using the printf
and scanf
functions.
Using a for
loop, the program calculates the factorial of the entered number.
Finally, the program uses the printf
function to output the factorial of the entered number to the user.
int main() {
int num, fact=1, i;
printf("Enter a number: ");
scanf("%d", &num);
for(i=1; i<=num; i++) {
fact = fact * i;
}
printf("Factorial of %d is %d", num, fact);
return 0;
}
Output:
Enter a number: 5
Factorial of 5 is 120


+ There are no comments
Add yours