The factorial of a positive integer n, denoted by n!, is the product of all positive integers less than or equal to n. Factorial does not exist for negative numbers and factorial of 0 is 1. Its most basic occurrence is the fact that there are n! ways to arrange n distinct objects into a sequence.
6! = 6 * 5 * 4 * 3 * 2 * 1 = 720.
C program to calculate factorial of a number using loop
In this program we first take N as input from user. Then using a for loop we calculate product of all numbers from 1 to N. Here we are using for loop but this program also can be solved in similar way using while loop. After calculating value of N! we print it on screen using printf function.
#include <stdio.h> int main(){ int N, nFactorial, counter; printf("Enter a number for factorial calculation \n"); scanf("%d",&N); for(counter=1,nFactorial=1; counter <= N; counter++){ nFactorial = nFactorial * counter; } printf("Factorial of %d is %d", N, nFactorial); return 0; }Output
Enter a number for factorial calculation 5 Factorial of 5 is 120
Enter a number for factorial calculation 0 Factorial of 0 is 1
C program to calculate factorial of a number using function
In below program, we wrote a function called getFactorial which takes an integer N as input and returns it factorial value (N!). It also does input validation and prints error message for negative number, as we know factorial is not defined for negative numbers.
#include <stdio.h> int getFactorial(int N); int main(){ int N, nFactorial = 0; printf("Enter a number\n"); scanf("%d",&N); nFactorial = getFactorial(N); if(nFactorial != 0){ printf("Factorial of %d is %d", N, nFactorial); } return 0; } int getFactorial(int N){ if(N < 0){ printf("Invalid Input: factorial not defined for negative numbers\n"); return 0; } int nFactorial, counter; /* N! = N*(N-1)*(N-2)*(N-3)*.....*3*2*1 */ for(counter=1,nFactorial=1; counter <= N; counter++){ nFactorial = nFactorial * counter; } return nFactorial; }Output
Enter a number 6 Factorial of 5 is 720
Enter a number for factorial calculation -3 Invalid Input: factorial not defined for negative numbers
C Program to find factorial of a number using recursion
We can use recursion to calculate factorial of a number because factorial calculation obeys recursive sub-structure property. Let factorial(N) is a function to calculate and return value of N!. To find factorial(N) we can first calculate factorial(N-1) then multiply it with N.
N! = 1 x 2 x 3 x 4....x (N-2) x (N-1) x N
N! = (N-1)! x N
factorial(N) = factorial(N-1) x N
Function factorial(N) reduces the problem of finding factorial of a number N into sub-problem of finding factorial on N-1. It keeps on reducing the domain of the problem until N becomes zero.
#include <stdio.h> int getFactorial(int N); int main(){ int N, nFactorial, counter; printf("Enter a number for factorial calculation \n"); scanf("%d",&N); printf("Factorial of %d is %d", N, getFactorial(N)); return 0; } int getFactorial(int N){ /* Exit condition to break recursion */ if(N <= 1){ return 1; } return N * getFactorial(N - 1); }Related Topics