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):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
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!