Factorial in C Programming

This post presents multiple C program for calculating the factorial of a number, using different approaches such as recursion, function, for loop, and while loop.

The programs are designed to handle non-negative integers and to correctly calculate the factorial of 0 and 1.

lady doing program of Factorial in C Programming
lady doing program of Factorial in C Programming

The post provides explanations for each approach and code examples, along with sample outputs for the programs.

These C programs offer flexibility in choosing the most appropriate method for solving specific problems. Regardless of the approach chosen, these programs provide a good foundation for understanding the basic concepts involved in calculating factorials in C programming.

A C program for factorial is a program that calculates the factorial of a given integer number. Factorial of a number is the product of all the positive integers from 1 to that number.

For example, the factorial of 5 is 5x4x3x2x1 = 120. The program can be implemented using a loop or a recursive function. The loop iterates from 1 to the given number, and the product of all the integers in the loop is the factorial.

 doing program of Factorial in C Programming
Programmer working in a software developing company office

The recursive function calls itself with the decremented value of the input number until the base case is reached. The base case is usually when the input number is 0 or 1, and the factorial value is 1.

The output of the program is the factorial value of the input number.

  • Factorial is a mathematical operation that is often used in probability and combinatorics. It is represented by the symbol “!” and is defined as the product of all positive integers from 1 to n, where n is the given integer number.
  • The factorial function is defined recursively as follows:
    • Base case: factorial of 0 is 1
    • Recursive case: factorial of n is n times factorial of (n-1)
  • In C programming, a program to calculate the factorial of a number can be implemented using a loop or a recursive function.
  • The loop approach is generally simpler and more efficient, as it does not require the overhead of function calls. The loop iterates from 1 to the given number, multiplying each integer by the previous result to get the factorial value.

Here’s an example for

c program for factorial using recursion

This program prompts the user to enter a non-negative integer, and then calls the factorial function to calculate the factorial of that number. The factorial function is defined recursively, which means that it calls itself to calculate the factorial of smaller and smaller numbers until it reaches the base case of 0, at which point it returns 1.

Let’s go through the factorial function in more detail. First, it checks if the input n is equal to 0. If it is, then it returns 1, because the factorial of 0 is defined to be 1. If n is not equal to 0, then it calculates the factorial of n-1 by calling factorial(n-1), and multiplies that result by n to get the factorial of n.

For example, let’s say the user enters 5 as the input. The factorial function will be called with n=5, and will calculate 5 * factorial(4). To calculate factorial(4), the function will call itself with n=4, and will calculate 4 * factorial(3). This process will continue until the base case of n=0 is reached, at which point the function will start returning values: factorial(0) will return 1, factorial(1) will return 1 * 1 = 1, factorial(2) will return 2 * 1 = 2, factorial(3) will return 3 * 2 = 6, factorial(4) will return 4 * 6 = 24, and finally factorial(5) will return 5 * 24 = 120. Therefore, the program will output “Factorial of 5 is 120”.

#include <stdio.h>

int factorial(int n);

int main() {
    int n;
    printf("Enter a non-negative integer: ");
    scanf("%d", &n);
    printf("Factorial of %d is %d\n", n, factorial(n));
    return 0;
}

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

output
Enter a non-negative integer: 5
Factorial of 5 is 120
c program for factorial using recursion
c program for factorial using recursion
output for factorial using recursion
output for factorial using recursion

factorial program in c using function

The program prompts the user to enter a non-negative integer, and then calls the factorial function to calculate the factorial of that number.

The factorial function takes an integer num as an argument and returns the factorial of that number. The function initializes a variable fact to 1, and then uses a for loop to multiply fact by each integer from 1 up to and including num. Finally, the function returns the value of fact.

For example, if the user enters 5 as input, the factorial function multiplies fact by each integer from 1 to 5 in turn, and then returns the final value of fact, which is 120. Therefore, the program outputs “Factorial of 5 is 120”.

#include <stdio.h>

int factorial(int num);

int main() {
    int num;
    printf("Enter a non-negative integer: ");
    scanf("%d", &num);
    printf("Factorial of %d is %d\n", num, factorial(num));
    return 0;
}

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


OUTPUT
Enter a non-negative integer: 5
Factorial of 5 is 120
factorial program in c using function
factorial program in c using function
output for factorial program in c using function
output for factorial program in c using function

Read more :- C PROGRAM TO GENERATE THE FIBONACCI SERIES

factorial program in c using for loop

The program prompts the user to enter a non-negative integer, and then uses a for loop to calculate the factorial of that number.

The for loop initializes a variable i to 1, and then iterates over the loop as long as i is less than or equal to num. In each iteration of the loop, the variable fact is multiplied by i, and then i is incremented by 1. Finally, the loop exits, and the program outputs the value of fact.

For example, if the user enters 5 as input, the for loop multiplies fact by each integer from 1 to 5 in turn, and then outputs the final value of fact, which is 120. Therefore, the program outputs “Factorial of 5 is 120”.

#include <stdio.h>

int main() {
    int num;
    int fact = 1;
    printf("Enter a non-negative integer: ");
    scanf("%d", &num);
    for (int i = 1; i <= num; i++) {
        fact *= i;
    }
    printf("Factorial of %d is %d\n", num, fact);
    return 0;
}

output
Enter a non-negative integer: 5
Factorial of 5 is 120
factorial program in c using for loop
factorial program in c using for loop
output for factorial program in c using for loop
output for factorial program in c using for loop

read more :- C PROGRAM TO CHECK WHETHER A NUMBER IS AN ARMSTRONG NUMBER OR NOT

factorial program in c using while loop

The program prompts the user to enter a non-negative integer, and then uses a while loop to calculate the factorial of that number.

The while loop initializes a variable i to 1, and then continues to iterate over the loop as long as i is less than or equal to num. In each iteration of the loop, the variable fact is multiplied by i, and then i is incremented by 1. Finally, the loop exits, and the program outputs the value of fact.

For example, if the user enters 5 as input, the while loop multiplies fact by each integer from 1 to 5 in turn, and then outputs the final value of fact, which is 120. Therefore, the program outputs “Factorial of 5 is 120”.

#include <stdio.h>

int main() {
    int num;
    int fact = 1;
    int i = 1;
    printf("Enter a non-negative integer: ");
    scanf("%d", &num);
    while (i <= num) {
        fact *= i;
        i++;
    }
    printf("Factorial of %d is %d\n", num, fact);
    return 0;
}

output

Enter a non-negative integer: 5
Factorial of 5 is 120
factorial program in c using while loop
factorial program in c using while loop
output for factorial program in c using while loop
output for factorial program in c using while loop

Overall, this post is a valuable resource for anyone seeking to write a C program for calculating the factorial of a number, and provides clear explanations and examples of each approach, making it easy for beginners to follow along.

Read More :- C PROGRAM TO CHECK WHETHER A GIVEN NUMBER IS A PALINDROME OR NOT

Deep Mistry

Digital Marketing Consultants and Social Media Marketing Expert with over 3 years of rich experience in various Branding, Promotions, business directories, On pages and off page optimization, Link building Advertising, Research, paid advertisement, content writing and marketing

16 thoughts on “A Step by Step Guide for Completing Factorial in C Programming

Leave a Reply

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