C Program to Find Factorial of a Number!

Here is another interesting program for school students who are interested in learning C programming.

This program will calculate the factorials and print all the values as a factorial pyramid.

Example (C Program to find factorial of a number):

#include <stdio.h>
int main()
{
	int num, fact = 1, i;
		
	//print about the program
	printf("C Program to calculate factorial..\n");
    printf("Enter Number to calculate factorial: ");
   	
	//get the input value from the user
	scanf("%d", &num);
		
	//loop till the iterator $i equals to $number
	for (i = 1; i <= num; i++){
		
		//formula to calculate factorial is to 
		//multiply the iterator i value with fact value.			
		fact = fact * i;
		
		//print fact to show the factorial pyramid
		//or put this line out of this 'for loop' to show only the total value
		printf("%d\n", fact);
	}
    return 0;
}

Read the comments in the program to understand how it works.

The output of the above C program will be:

C Program to calculate factorial..
Enter Number to calculate factorial: 12
1
2
6
24
120
720
5040
40320
362880
3628800
39916800
479001600

Enjoy the day!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top