Subscribe to:
Post Comments (Atom)
CodeShikhi - The World for Programmers, প্রোগ্রামিং,Learn Programming,Learn Javascirpt | C | C++ | Python | OOP,Learn Programming in Bangla,Bangla Programming Tutorial,Leetcode Problem Solution,BEE Problem Solution,C Programming in Bangla,C in Bangla,Javascript tutorials,C Programming Bangla Tutorial, URI Online Judge Solution in C C++ Python,JavaScript Full Tutorials , OOP Tutorial in C++, Object Oriented Programming in C++,Learn OOP in C++,C Programming Exercise,C Programming Example with Code
#include<stdio.h>
int 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);
}
Initializes :
number-for storing given number
factorial-for storing factorial of a number.
i for using in loop
Prompting user for input through scanf statement.
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
There fore it prints the factorial which is 120.
Finally print the result.
Previous Post:
0 Response to Write a C Program to Print Factorial of Given Number | C Programming Examples
Post a Comment