factorial of a number is the product of all the integers between 1 and that number. For example, 4 factorial is 4 * 3 * 2 * 1. This can also be expressed as 4! = 4 * 3! where ‘!’ stands for factorial. Thus factorial of a number can be expressed in the form of itself. Hence this can be programmed using recursion
Factorial notation
The notation n! represents the product of first n natural numbers, i.e., the product 1 × 2 × 3 × . . . × (n – 1) × n is denoted as n!. We read this symbol as ‘n factorial’. Thus, 1 × 2 × 3 × 4 . . . × (n – 1) × n = n !
1 = 1 !
1 × 2 = 2 !
1× 2 × 3 = 3 !
1 × 2 × 3 × 4 = 4 ! and so on.
Example of factorial programme using c
#include <stdio.h>
int main( )
{
int a, fact ;
printf ( "\nEnter any number " ) ;
scanf ( "%d", &a ) ;
fact = factorial ( a ) ;
printf ( "Factorial value = %d", fact ) ;
return 0;
}
factorial ( int x )
{
int f = 1, i ;
for ( i = x ; i >= 1 ; i-- )
f = f * i ;
return ( f ) ;
}
And here is the output…
Enter any number 3
Factorial value = 6