The factorial of a non-negative integer nn is the product of all positive integers less than or equal to nn. The factorial of nn is denoted as n!n!.

For example:

  • 5!=5×4×3×2×1=1205! = 5 \times 4 \times 3 \times 2 \times 1 = 120
  • 0!=10! = 1 (by definition)

Logic to Calculate Factorial:

  1. If nn is 0, return 1 (base case).
  2. For n>0n > 0, multiply nn by the factorial of n1n-1.
  3. This can be done using either a recursive or iterative approach.

Program:

Here’s a simple C program to calculate the factorial of a number using both iterative and recursive methods.

Iterative Approach:

#include <stdio.h> int main() { int n, i; unsigned long long factorial = 1; // Use long long to handle larger values // Input a non-negative integer from the user printf("Enter a non-negative integer: "); scanf("%d", &n); // Check for negative input if (n < 0) { printf("Factorial is not defined for negative numbers.\n"); } else { // Iteratively calculate the factorial for (i = 1; i <= n; ++i) { factorial *= i; // Multiply current value of factorial by i } printf("Factorial of %d = %llu\n", n, factorial); } return 0; }

Recursive Approach:

#include <stdio.h> // Function to calculate factorial recursively unsigned long long factorial(int n) { if (n == 0) // Base case: 0! = 1 return 1; else // Recursive case return n * factorial(n - 1); } int main() { int n; // Input a non-negative integer from the user printf("Enter a non-negative integer: "); scanf("%d", &n); // Check for negative input if (n < 0) { printf("Factorial is not defined for negative numbers.\n"); } else { printf("Factorial of %d = %llu\n", n, factorial(n)); } return 0; }

Explanation:

  1. Variables:

    • int n: Holds the non-negative integer input by the user.
    • unsigned long long factorial: Stores the computed factorial value. Using unsigned long long allows handling larger results.
  2. Input:

    • The user is prompted to enter a non-negative integer.
  3. Negative Input Check:

    • The program checks if nn is negative and informs the user that factorials for negative numbers are not defined.
  4. Iterative Approach:

    • A for loop runs from 1 to nn, multiplying the factorial variable by each integer ii.
  5. Recursive Approach:

    • The factorial function calls itself, reducing nn by 1 each time until it reaches the base case of 0.
  6. Output:

    • The program prints the calculated factorial.

Sample Output:

Example 1 (Iterative):

Enter a non-negative integer: 5 Factorial of 5 = 120

Example 2 (Recursive):

Enter a non-negative integer: 7 Factorial of 7 = 5040

Key Points:

  • Base Case: In the recursive approach, the base case (0! = 1) is crucial to prevent infinite recursion.
  • Data Type: unsigned long long is used to accommodate larger factorial values, especially for numbers greater than 20, where the factorial exceeds the limits of standard data types.
  • Factorial Growth: Factorials grow very quickly, which is an important consideration in both iterative and recursive implementations.