Summary: In this example, we will learn different ways to find the factorial of a given number in C programming language.

Factorial of any number (n) is given by the following formula:

We can easily calculate it using the following 4 ways in C:

Method 1: Using While Loop

To calculate factorial of a number using while loop we need to run while loop until n!=0, each time decreasing the value of n by 1 and multiplying the number to the resultant value.

Output:

C Program for Factorial using While Loop

Method 2: Using For Loop

To calculate the factorial of a number using for loop, we run a loop from n to 1 in decrementing order and multiply each number into the factorial product.

Output:

C Program for Factorial using for Loop

Method 3: Using Recursion

We can also find the factorial of any positive number using recursion. With base condition n==1, we multiply the number with the next recursive call as n * factorial(n-1).

Output:

C Program for Factorial using recursion

Method 4: Using Function

We create a function that applies any of the above three methods to calculate and return the factorial of the passed number as an argument.

Output:

C Program for Factorial using function

There were four ways using which ae can compute the factorial of any positive number in C programming. Comment below your suggestion.

This Post Has One Comment

Leave a Reply

2 × 1 =