Calculate factorial of a number
C program to find the factorial of a number entered by the user.
The Factorial of a non-negative number is the product of all numbers, which are less than or equal to that number, and greater than 0.
i.e. Factorial of n is the multiplication of all the integers from 1 to n.
In mathematic representation, factorial is represented by ! sign.
n! = n * (n-1) * (n-2) * .... * 1
5! = 1 * 2 * 3 * 4 * 5 = 120
Factorial of negative numbers is not defined.
Factorial of 0 is 1. i.e. 0! = 1.
i.e. Factorial of n is the multiplication of all the integers from 1 to n.
In mathematic representation, factorial is represented by ! sign.
n! = n * (n-1) * (n-2) * .... * 1
5! = 1 * 2 * 3 * 4 * 5 = 120
Factorial of negative numbers is not defined.
Factorial of 0 is 1. i.e. 0! = 1.
Program
#include<stdio.h>
#include<conio.h>
void main()
{
int num,i;
unsigned long long fact=1;
printf("Enter the number whose factorial is to be calculated\t:");
scanf("%d",&num);
if(num<0)
{
printf("Factorial of negative numbers is not defined.");
exit(0);
getch();
}
else if(num==0)
fact=1;
else
{
for(i=1;i<=num;i++)
{
fact = fact * i;
}
}
printf("factorial is %llu",fact);
getch();
}
Output
Enter the number whose factorial is to be calculated :5
factorial is 120