Skip to content
Home » Programming » C Examples » C Program to find the Factorial of a number

C Program to find the Factorial of a number

C Program to find the factorial of a number

In this example, we will learn to write a program to find the factorial of a number in C. This example will be using loops and conditional statements to find the factorial.

What is a factorial?

The factorial of a non-negative number is the product of all the natural numbers which are less than or equal to that number. From definition, we can say that the factorial of a number is always a positive natural number.

Example:

Factorial of 5 = 1 * 2 * 3 * 4 * 5 = 120
Factorial of 6 = 1 * 2 * 3 * 4 * 5 * 6 = 720

The factorial of a number is denoted by the number followed by the explanation mark.

So above example can be written as,

5! = 1 * 2 * 3 * 4 * 5 = 120
6! = 1 * 2 * 3 * 4 * 5 * 6 = 720

From the definition, the factorial of a number can generalized as,

N! = 1 * 2 * 3 * . . . * (N - 2) * (N - 1) * N

Moreover, the factorial of 0 is always 1. Why is it so? You can read it about it here.

0! = 1

Likewise, the factorial of 1 is also 1,

1! = 1

To find the factorial, a number must not be negative or in decimals.

Algorithm to find the factorial of a number

We will use the variable “num” to store the value of the entered number. Algorithm is given as below,

Step 1: Start.
Step 2: Take an integer number as input in variable num and initialize i = 1.
Step 3: Check if the num is less than 0. If yes,
            Display “Factorial of a negative number does not exist.”
            And go to step 6
Step 4: Repeat until i is less than or equals to  num,
            fact = fact * i
            i = i + 1
Step 5: Display factorial of the given number.
Step 6: End

In Short,

Start,
    Take input in num
    i = 1
    if num < 0,
        Display “Factorial of a negative number does not exist.”
    else
        Repeat until i <= num,
            fact = fact * i
            i = i + 1
        Display factorial of number
End.

Also Read: C Program to Swap two numbers without Using a Temporary Variable

Program to find the factorial of a number using for loop

#include <stdio.h>
int main() {
    int num, i;
    unsigned long fact = 1;
    
    printf("Enter an integer: ");
    scanf("%d", &num);

    if (num < 0)
        printf("Factorial of negative number doesn't exist.");
    else {
        for (i = 1; i <= num; i++) {
            fact = fact * i;
        }
        printf("Factorial of %d = %lu", num, fact);
    }

    return 0;
}

Output

Enter an integer: 6                      
Factorial of 6 = 720
Enter an integer: 0
Factorial of 0 = 1
Enter an integer: 1
Factorial of 1 = 1

Explanation

  • We have used unsigned long data type for fact variables because factorial cannot be negative and the product of all numbers can be very large.
  • At first, we checked if the number is negative or not. This is important since we cannot find the factorial of  a  negative number. We checked this by the condition,
num < 0

This is used with the if statement.

if (num < 0)
    . . .
  • If number is positive, we loop until i <= num,
for (i = 1; i <= num; i++) {
    . . . 
}
  • Then we multiply each number with fact until we reach the number itself,
fact = fact * i

This can also be written as,

fact *= i
  • If the number is 0, control does not go inside the for loop. This is because the condition i<=num is not satisfied.
For 0,
    1 <= 0 // since i = 1 and num = 0
    So, condition is not satisfied and control is not transferred to the loop

So, the initial value of fact i.e. 1 is the factorial of 0.

  • However, for 1 control goes inside the loop since condition is satisfied.
For 1,
    1 <= 1 // since i = 1 and num =1
    So, condition is satisfied and control is transferred to the loop

But it runs only once,

For i = 1,
    fact = 1 * 1 // since, initially fact = 1 and i = 1

Therefore, fact = 1. This means that, factorial of 1 is 1.

Also Read: C program to find the size of data types using sizeof operator

Program using while loop

#include <stdio.h>
int main() {
    int num, i=1;
    unsigned long fact = 1;
    
    printf("Enter an integer: ");
    scanf("%d", &num);

    if (num < 0)
        printf("Factorial of negative number doesn't exist.");
    else {
        while(i<=num) {
            fact = fact * i;
	        i++;
        }
        printf("Factorial of %d = %lu", num, fact);
    }

    return 0;
}

Output

Enter an integer: 5
Factorial of 5 = 120

Explanation

  • Finding factorial of a number using while loop is same as for loop. It has slightly different syntax and everything is same.
while(i <= num) {
    . . .
    i++;
}

Here, at first line we are checking the condition if the counter i.e. i is less than or equal to num.

While in for loop we increment/decrement value along with checking condition in while loop it is done in different place.

  • Other steps are same as for loop.

Also Read: C Program to Find ASCII Value of a Character & Vice Versa

Program Using do…while loop

#include <stdio.h>
int main() {
    int num, i=1;
    unsigned long fact = 1;
    
    printf("Enter an integer: ");
    scanf("%d", &num);

    if (num < 0)
        printf("Factorial of negative number doesn't exist.");
    else {
        do {
            fact = fact * i;
            i++;
        } while(i<=num);
        printf("Factorial of %d = %lu", num, fact);
    }

    return 0;
}

Output

Enter an integer: 7
Factorial of 7 = 5040

Explanation

  • do…while implementation is same as while implementation it is only different in syntax.
    In while loop we check at the beginning of the loop but in do…while we check at the end of the loop.
do {
    fact = fact * i;
    i++;
} while(i <= num);

More Examples

Kth largest factor of N in C and python with explanation
Divine Divisors Program using C

Leave a Reply

Your email address will not be published. Required fields are marked *