C Program to Find Factorial of a Number

CodingTute

Updated on:

C Program to Find Factorial of a Number

Factorial program in C: In this article, you will explore how to find the factorial of a given number in C.

In C programming factorial can be found for the given number using the following methods.

  • Factorial of a number using loop
  • Factorial of a number using recursion

Also check: C Programming Examples

What is a Factorial?

A factorial is a mathematical function which calculates the product of all positive integers that are smaller than or equal to a given positive integer. It is denoted by an exclamation mark (!).

For example, the factorial of 5 is written as 5! and can be calculated as 5 x 4 x 3 x 2 x 1 = 120. Factorials are often used in probability theory and combinatorics to determine the number of possible permutations and combinations of a given set of elements.

The factorial of a number n can be defined as n!. Where n! = n x (n-1) x (n-2) x … x 1

Factorial Program in C

To find the factorial of a number, we need to multiply all the positive integers that come before it. For instance, to find the factorial of 5, we need to multiply 5 by 4, then the product of these two numbers (20) by 3, and so on until we reach 1.

We can use a loop to multiply the numbers from 1 to the given number, i.e., 1 to n. In each iteration of the loop, we multiply the current number by the result (initially set to 1). After all iterations, the result will be the factorial of the given number.

Factorial of a number using loop

#include <stdio.h>

int main()
{
    unsigned long long fact = 1;
    int num, i;
    printf("Enter Number: ");
    scanf("%d", &num);
    if(num < 0)
       printf("Enter Positive Number");
    else{
       //calculating factorial
       for(int i=2; i<=num; i++){
           fact *= i;
       }
    
   printf("Factorial of %d is %llu", num, fact);
    }
    return 0;
}
Online C Compiler

Explanation

for(int i=2; i<=num; i++){
        fact *= i;
}

Here in the loop, we initiated with i=2, not i=1 because any number multiplied with 1 gives the same number and also fact variable is initiated with 1. So no need to run the loop for i=1.

Factorial of a number using recursion

#include <stdio.h>

unsigned long long factorial(int n){
    if(n == 0)
        return 1;
    return n * factorial(n-1);
}

int main()
{
    int num;
    printf("Enter Number: ");
    scanf("%d", &num);
    if(num < 0)
       printf("Enter Positive Number");
    else
       printf("Factorial of %d is %llu", num, factorial(num));

    return 0;
}
Online C Compiler

Output

Enter Number: 5
Factorial of 5 is 120

Explanation

Once the factorial(num) function is called from the main function, the factorial function calls itself by passing n-1 value to the function and multiplied by n until n value reaches 0 in the next function call.

If the n value reaches 0 in the functional scope, it starts returning the value resulting in the calculation of the factorial of the given number.

Follow us on Facebook, YouTube, Instagram, and Twitter for more exciting content and the latest updates.

Exit mobile version