Factorial Number
FACTORIAL NUMBER
The factorial of a positive integer n, denoted by n!, is the product of all positive integers less than or equal to n. For example,
The value of 0! is 1, according to the convention for an empty product
Mathematically, the formula for the factorial is as follows. If n is an integer greater than or equal to 1, then
n ! = n ( n - 1)( n - 2)( n - 3) ... (3)(2)(1)
If p = 0, then p ! = 1 by convention.
We can easily calculate a factorial from the previous one:
As a table:
n | n! | ||
---|---|---|---|
1 | 1 | 1 | 1 |
2 | 2 × 1 | = 2 × 1! | = 2 |
3 | 3 × 2 × 1 | = 3 × 2! | = 6 |
4 | 4 × 3 × 2 × 1 | = 4 × 3! | = 24 |
5 | 5 × 4 × 3 × 2 × 1 | = 5 × 4! | = 120 |
6 | etc | etc |
C Program for Factorial of a Number
#include <stdio.h>
int main()
{
int n, i;
unsigned long long factorial = 1;
printf("Enter an integer: ");
scanf("%d",&n);
// show error if the user enters a negative integer
if (n < 0)
printf("Error! Factorial of a negative number doesn't exist.");
else
{
for(i=1; i<=n; ++i)
{
factorial *= i; // factorial = factorial*i;
}
printf("Factorial of %d = %llu", n, factorial);
}
return 0;
}
Output:
Enter an integer: 10 Factorial of 10 = 3628800
Comments
Post a Comment