C Program to Find Factorial of a Number

  • Write a C program to find factorial of a number.

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.

N! = 1*2*3*4*.... *(N-2)*(N-1)*N
For Example
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.

/*
* C Program to print factorial of a number
*/
#include <stdio.h>
#include <conio.h>

int main(){
    int N, nFactorial, counter;
    printf("Enter a number for factorial calculation \n");
    scanf("%d",&N);
    /*
     * N! = N*(N-1)*(N-2)*(N-3)*.....*3*2*1 
     */
    for(counter = 1, nFactorial = 1; counter <= N; counter++){
        nFactorial = nFactorial * counter;
    }
    printf("Factorial of %d is %d", N, nFactorial);
    
    getch();
    return 0;
}
Program 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.

/*
* C Program to print factorial of a number using function
*/
#include <stdio.h>
#include <conio.h>

int getFactorial(int N);
int main(){
    int N, nFactorial = 0;
    printf("Enter a number for factorial calculation \n");
    scanf("%d",&N);
    nFactorial = getFactorial(N);
    
    if(nFactorial != 0){
        printf("Factorial of %d is %d", N, nFactorial);
    }    
    getch();
    return 0;
}

/*
 * Function to calculate factorial of a number 
 */
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;
}
Program Output
Enter a number for factorial calculation 
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.

/*
* C Program to print factorial of a number using recursion
*/
#include <stdio.h>
#include <conio.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));
   
    getch();
    return 0;
}
 
/*
 * Recursive function to calculate factorial of a number
 */
int getFactorial(int N){
    /* Exit condition to break recursion */
    if(N <= 1){
         return 1;
    }
    /*  N! = N*(N-1)*(N-2)*(N-3)*.....*3*2*1  */  
    return N * getFactorial(N - 1);
}

Related Topics
C program to check string is palindrome
C program to check armstrong number
C Program to print fibonacci series
C program to convert string to integer
C program to check string is palindrome
C program to find a substring from a given string
C program to find hcf and lcm of two numbers
List of all C programs