TASK-
Write a complete and an efficient C program that takes an integer number from the user and calculates its factorial.
NOTE: There are two possibilities:
- The Entered number is 0
In this possibility the factorial will be 1.
2. The Entered number is non zero.
for example: Entered number is 4 factorial would be equal to 24.
CODE:
#include<stdio.h>
int main(){
int fact=1,num,i; //fact is taken for factorial
printf("Enter the number ");
scanf("%d",&num);
if (num==0) //First possibility
{
printf("The factorial of %d is 1",num);
}
else //Second possibility
{
for (i=1;i<=num;i++)
/* logic of factorial = loop gets start from 1 and goes till num,if we break num (e.g num=3) 1*2*3 would be factorial same thing happening here fact = 1 and it gets multiplied by i, i=1 then i=2..... and upto num*/
{
fact=fact*i;
}
printf("The factorial of %d is %d",num,fact);
}
getchar();
return 0;
}