C Program to Find Factorial of a Number

Howdy readers, today you will learn how to write a program to find the factorial of a number using C Programming language.

C Program to Find Factorial of a Number

The factorial of a number is the product of all integers from 1 to that number. For example: the factorial of 6(denoted as 6!) is 1*2*3*4*5*6 = 720.

We will be using the following methods to find the factorial of a number.

  1. Using For Loop
  2. Using While Loop
  3. Using Functions
  4. Using Recursion

So, without any delay, let’s begin this tutorial.

C Program to Find Factorial of a Number

C Program

// C Program to Find Factorial of a Number
#include <stdio.h>
int main(){
    int num, fact = 1;
    
    // Asking for input
    printf("Please enter a number: ");
    scanf("%d", &num);
    
    // Calculating Factorial
    for (int i = 1; i <= num; i++){
        fact = fact * i;
    }
    
    // Displaying output
    printf("Factorial of %d is: %d", num, fact);
    
    return 0;
}

Output

Please enter a number: 3
Factorial of 3 is: 6
C Program to Find Factorial of a Number

Explanation

    int num, fact = 1;

Here, in this program we have declared two int data type variables named num and fact.

    // Asking for input
    printf("Please enter a number: ");
    scanf("%d", &num);

Then, the user is asked to enter a number. This number gets stored in the num named variable.

    // Calculating Factorial
    for (int i = 1; i <= num; i++){
        fact = fact * i;
    }

We initialize a for loop with i = 1, this loop keeps executing until i is greater than num.

Suppose, the user enter a number 3, then:

i  <= num    fact * i = fact    
1 <= 3 1 * 1 = 1
2 <= 31 * 2 = 2
3 <= 32 * 3 = 6
4 > 3loop terminates

So, we get 3! = 6.

    // Displaying output
    printf("Factorial of %d is: %d", num, fact);

And finally, the factorial of the entered number is displayed on the screen using printf() function.

C Program to Find Factorial of a Number Using While Loop

C Program

// C Program to Find Factorial of a Number Using While Loop
#include <stdio.h>
int main(){
    int i = 1, num, fact = 1;
    
    // Asking for input
    printf("Enter a positive number: ");
    scanf("%d", &num);
    
    // Finding Factorial
    while (i <= num){
        fact = i * fact;
        i++;
    }
    
    // Displaying output
    printf("Factorial of %d is: %d", num, fact);
    
    return 0;
}

Output

Enter a positive number: 10
Factorial of 10 is: 3628800
C Program to Find Factorial of a Number Using while Loop

Explanation

    // Finding Factorial
    while (i <= num){
        fact = i * fact;
        i++;
    }

In this program, we have used a while loop to calculate the factorial of the entered number.

C Program to Find Factorial of a Number Using Functions

C Program

// C Program to Find Factorial of a Number Using Functions
#include <stdio.h>
int fact(int num){
    int i, factorial = 1;
    for (i = 1; i <= num; i++){
        factorial = factorial * i;
    }
    return factorial;
}

int main(){
    int num, f;
    
    // Asking for input
    printf("Please enter a number: ");
    scanf("%d", &num);
    
    // Calling out custom function
    f = fact(num);
    
    // Displaying output
    printf("Factorial of %d is: %d", num, f);
    
    return 0;
}

Output

Please enter a number: 4
Factorial of 4 is: 24
C Program to Find Factorial of a Number Using Function

Explanation

int fact(int num){
    int i, factorial = 1;
    for (i = 1; i <= num; i++){
        factorial = factorial * i;
    }
    return factorial;
}

In this program, we have defined a custom function named fact which computes and returns the factorial of a number using a for loop.

C Program to Find Factorial of a Number Using Recursion

C Program

// C Program to Find Factorial of a Number Using Recursion
#include <stdio.h>
int recursive_fact(int num){
    if (num == 0 || num == 1){
        return 1;
    }
    else{
        return (num * recursive_fact(num - 1));
    }
}

int main(){
    int num, factorial;
    
    // Asking for input
    printf("Please enter a number: ");
    scanf("%d", &num);
    
    // Calling out custom function
    factorial = recursive_fact(num);
    
    // Displaying output
    printf("Factorial of %d is: %d", num, factorial);
    
    return 0;
}

Output

Please enter a number: 6
Factorial of 6 is: 720
C Program to Find Factorial of a Number Using Recursion

Explanation

int recursive_fact(int num){
    if (num == 0 || num == 1){
        return 1;
    }
    else{
        return (num * recursive_fact(num - 1));
    }
}

Here, we have defined a recursive function named recursive_fact which returns 1 if the argument passed is 0 or 1. Otherwise, it returns a recursive call again.

Conclusion

I hope after reading this post, you understand how to write a program to find the factorial of a number using C Programming language.

If you face any difficulty while understanding this program, then let us know in the comment section. We will be glad to solve your query.

Leave a Comment