#include<stdio.h>
#include<conio.h>
void main()
{
int num,i;
long fact=1;
clrscr();
printf("\n Enter Any Number: ");
scanf("%d",&num);
if(num<=0)
printf("\n Invalid Number! ");
else
{
fact=num;
i=1;
while(i<num)
{
fact=fact*i;
i++;
}
printf("\n Factorial = %ld",fact);
}
getch();
}
Output:
C Program To Find Factorial Of A Number Using For Loop
#include<stdio.h>
#include<conio.h>
void main()
{
int num,i;
long fact=1;
clrscr();
printf("\n Enter Any Number: ");
scanf("%d",&num);
if(num<=0)
printf("\n Invalid Number ! ");
else
{
fact=num;
for(i=1;i<num;i++)
{
fact=fact*i;
}
printf("\n Factorial = %ld",fact);
}
getch();
}
I found the appropriate explanation of the above program in the video below.
Find Factorial Of A Number Using Function
#include<stdio.h>
#include<conio.h>
void factorial();
void main()
{
clrscr();
factorial();
getch();
}
void factorial()
{
int i,n;
long fact=1;
printf("\n Enter Any Number: ");
scanf("%d",&n);
if(n<=0)
printf("\n Negative Number or Zero Not Allowed ! ");
else
{
fact=n;
for(i=1;i<n;i++)
{
fact=fact*i;
}
}
printf("\n Factorial = %ld",fact);
}
In this way, we can find factorial of any given number with the help of while loop, for loop as well as function.

