Contact Learn C
Copy

Program 16: To find factorial of a number

Program 16:
 
#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);
}
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
  4. There fore it prints the factorial which is 120.
Output:
factorial of a number

1 comment:

  1. factorial hundred In the last few days, the “factorial of 100” is one of the top subjects and a lot of maths geeks compute it using voice assistants such as Alexa, Shiri, etc.

    ReplyDelete

Donate

Download App and Learn when ever you want

Get it on PlayStore
Get it on Amazon App Store
Get it on Aptoide