Examples of C part 20 factorial of numbers

#include<stdio.h>
main()
{
  int number,factorial=1,i;
  printf("Enter a number for knowing it's factorial\n");
  scanf("%d",&number);
  for(i=1;i<=number;i++)
  {
   factorial=factorial*i;
  }
  printf("%d!=%d\n",number,factorial);
}
Output:
Enter a number for knowing it's factorial
5
5!=120
Explanation:
  1. Initializes :
    • number-for storing given number
    • factorial-for storing factorial of a number.
    • i for using in loop
  2. Prompting user for input through scanf statement.
  3. The main logic goes here:(factorial=factorial*i)
    • i=1 and i<=number(say 5) which is true then factorial=1*1=1 then i increments by 1
    • Now i=2 and as i<=5 which is true then factorial=1*2=2.Now i increases by 1
    • Now i=3 and as i<=5 which is true then factorial=2*3=6.Now i increases by 1
    • Now i=4 and as i<=5 which is true then factorial=6*4=24.Now i increases by 1
    • Now i=5 and as i<=5 which is true then factorial=24*5=120.Now i increases by 1
    • Now i=6 and as I is not less or equal to 5 the loop terminates
  1. There fore it prints the factorial which is 120.