Factorial of a number in c programming
Factorial of a number in c programming:-#include<studio.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
5
5!=120
Explanation:-
- number-for storing given number
- factorial-for storing factorial of a number.
- i for using in loop
- 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

Comments
Post a Comment