C Program to Calculate the Factorial of a Number

Finding the factorial of a number is a common problem in programming. A factorial of a non-negative integer n is the product of all positive integers less than or equal to n. This article will cover various methods to compute the factorial of a number in C. We will explore multiple C Program to Calculate the Factorial of a Number.

Prerequisites

Before we delve into the examples, ensure you have the following prerequisites:

  • A C compiler (such as GCC)
  • A text editor or IDE for writing your C code
  • Basic understanding of C programming concepts, especially loops and functions

1. Calculating the Factorial of a Number

In this section, we will look at several methods to compute the factorial of a number in C.

1.1 Using a for Loop

Example 1: Compute the Factorial Using a for Loop

This method uses a for loop to compute the factorial of a number iteratively.

Code

C
#include <stdio.h>

int main() {
    int n;
    unsigned long long factorial = 1;

    printf("Enter a positive integer: ");
    scanf("%d", &n);

    for (int i = 1; i <= n; ++i) {
        factorial *= i;
    }

    printf("Factorial of %d = %llu\n", n, factorial);

    return 0;
}

Explanation

  • Include necessary header: #include <stdio.h> for input/output functions.
  • Input the number: scanf reads a positive integer from the user.
  • Iterate using a for loop: The loop runs from 1 to n, multiplying each number to factorial.
  • Output the result: printf displays the factorial of the number.

Output

C
Enter a positive integer: 5
Factorial of 5 = 120

1.2 Using a while Loop

Example 2: Compute the Factorial Using a while Loop

This method uses a while loop to compute the factorial of a number iteratively.

Code

C
#include <stdio.h>

int main() {
    int n;
    unsigned long long factorial = 1;
    int i = 1;

    printf("Enter a positive integer: ");
    scanf("%d", &n);

    while (i <= n) {
        factorial *= i;
        ++i;
    }

    printf("Factorial of %d = %llu\n", n, factorial);

    return 0;
}

Explanation

  • Include necessary header: #include <stdio.h> for input/output functions.
  • Input the number: scanf reads a positive integer from the user.
  • Iterate using a while loop: The loop runs from 1 to n, multiplying each number to factorial.
  • Output the result: printf displays the factorial of the number.

Output

C
Enter a positive integer: 4
Factorial of 4 = 24

1.3 Using Recursion

Example 3: Compute the Factorial Using Recursion

This method uses recursion to compute the factorial of a number.

Code

C
#include <stdio.h>

unsigned long long factorial(int n);

int main() {
    int n;

    printf("Enter a positive integer: ");
    scanf("%d", &n);

    printf("Factorial of %d = %llu\n", n, factorial(n));

    return 0;
}

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

Explanation

  • Include necessary header: #include <stdio.h> for input/output functions.
  • Declare a recursive function: unsigned long long factorial(int n) to compute the factorial.
  • Input the number: scanf reads a positive integer from the user.
  • Call the recursive function: factorial(n) computes the factorial recursively.
  • Output the result: printf displays the factorial of the number.

Output

C
Enter a positive integer: 6
Factorial of 6 = 720

1.4 Using a Function with Iteration

Example 4: Compute the Factorial Using a Function with Iteration

This method uses a function with iteration to compute the factorial of a number.

Code

C
#include <stdio.h>

unsigned long long factorial(int n);

int main() {
    int n;

    printf("Enter a positive integer: ");
    scanf("%d", &n);

    printf("Factorial of %d = %llu\n", n, factorial(n));

    return 0;
}

unsigned long long factorial(int n) {
    unsigned long long result = 1;
    for (int i = 1; i <= n; ++i) {
        result *= i;
    }
    return result;
}

Explanation

  • Include necessary header: #include <stdio.h> for input/output functions.
  • Declare a function: unsigned long long factorial(int n) to compute the factorial iteratively.
  • Input the number: scanf reads a positive integer from the user.
  • Call the function: factorial(n) computes the factorial iteratively.
  • Output the result: printf displays the factorial of the number.

Output

C
Enter a positive integer: 7
Factorial of 7 = 5040

1.5 Using Arrays

Example 5: Compute the Factorial Using Arrays

This method stores intermediate results in an array to compute the factorial of a number.

Code

C
#include <stdio.h>

unsigned long long factorial(int n);

int main() {
    int n;

    printf("Enter a positive integer: ");
    scanf("%d", &n);

    printf("Factorial of %d = %llu\n", n, factorial(n));

    return 0;
}

unsigned long long factorial(int n) {
    unsigned long long result[n + 1];
    result[0] = 1;
    for (int i = 1; i <= n; ++i) {
        result[i] = i * result[i - 1];
    }
    return result[n];
}

Explanation

  • Include necessary header: #include <stdio.h> for input/output functions.
  • Declare a function: unsigned long long factorial(int n) to compute the factorial using arrays.
  • Input the number: scanf reads a positive integer from the user.
  • Call the function: factorial(n) computes the factorial using an array to store intermediate results.
  • Output the result: printf displays the factorial of the number.

Output

C
Enter a positive integer: 8
Factorial of 8 = 40320

2. Conclusion

In this article, we explored various methods to compute the factorial of a number in C: using a for loop, using a while loop, using recursion, using a function with iteration, and using arrays. Each method demonstrates different aspects of handling loops, functions, and arrays in C programming. By understanding these methods, you can choose the one that best fits your specific needs and enhance your skills in basic arithmetic operations and programming logic in C.

Using these examples as a guide, you can confidently compute the factorial of numbers in various ways depending on your requirements, making your programs more flexible and efficient.