Thursday, 17 March 2016

Write Factorial Program in C

Factorial Program in C

A factorial is the product of all the numbers from 1 to n, where n is the user-specified number.
Here we will write a factorial program in c in a very easy and simple way;

Factorial program in C

Find Factorial of Number Program in C

#include <stdio.h>
#include <conio.h>
void main()
{
int i, fact = 1, num;
printf("Enter any number: ");
scanf("%d", &num);
if (num <= 0)
fact = 1;
else
{
for (i = 1; i <= num; i++)
{
fact = fact * i;
}
}
printf("Factorial of %d = %5d\n", num, fact);
getch();
}

Output

Enter any number: 5
Factorial of 5 = 120

No comments:

Post a Comment