C programm to calculate factorial

, by Prashant Gunjal



Theory :

As we know that
1) 5! = 1*2*3*4*5
2) 3! = 1*2*3
3) 6! = 1*2*3*4*5*6

by observing above 3 examples we can built logic.We will accept number and then run for loop up to number times and simultaneously
we will calculate factorial

Program :

#include<stdio.h>
#include<conio.h>
void main()
{
int no,i,fact;
clrscr();
printf("\n\t Enter the number : ");

scanf("%d",&no);
fact = 1;
for(i=1;i<=no;i++)
{
fact = i * fact;
}
printf("\n\t Factorial of %d is = %d ",no,fact);
getch();
}

Output :

0 comments: