A Platform to LEARN | CODE | ACHIEVE
Connect :
Code
#include<stdio.h>
int main()
{
int n;
int i;
int fact=1;
printf("ENTER THE NUMBER :");
scanf("%d",&n;);
for(i=1;i<=n;i++)
{
fact=fact*i;
}
printf("THE FACTORIAL OF %d IS : %d",n,fact);
return 0;
}
Output
ENTER THE NUMBER :10
THE FACTORIAL OF 10 IS : 3628800
What we did - Our Approach
In this problem we have to generate the factorial of a number.
Let us consider an example :
Suppose we have to find the factorial of 6,hence this means that we have to calculate 1*2*3*4*5*6. It is clear that we can find the factorial of any number 'n',by multiplying each number from i:1 to 'n',such that fact=fact*i .This can be achieved by using for loop from 1 to 'n'.
Note : It is mandatory for you to initialize the result variable(here fact) with 1 as in an equation ' fact=fact*i ' , if fact=0 then overall result will be 0 as well.
See this code in :
Also see :
Participate in our MONTHLY CHALLENGE,and get a chance to get listed on our site and win the TITLE of "MR.GEEK".
Join us :
Connect :
SHARE:
For any Queries,suggestions,feel free to Mail us at :
[email protected]
For reporting any kind of copyright voilation,mail us at : [email protected]
© copyright CODE OF GEEKS