The "Factorial Using Recursion" program in C calculates the factorial of a given non-negative integer using a recursive function. Recursion is a programming technique where a function calls itself to solve smaller instances of the same problem. The factorial of a number nn (denoted as n!n!) is the product of all positive integers up to nn and is defined as follows:

  • n!=n×(n1)!n! = n \times (n-1)! for n>0n > 0
  • 0!=10! = 1 (the base case)

Implementation of Factorial Using Recursion

Here’s a simple implementation of the factorial program using recursion in C:

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

Explanation of the Program

  1. Header Files:

    • #include <stdio.h>: This header is included for standard input and output functions.
  2. Function Declaration:

    • long long factorial(int n): This function takes an integer n and returns its factorial as a long long integer. The return type is long long to accommodate larger results, especially for larger values of n.
  3. Base Case:

    • The base case for the recursive function checks if n is 0. If it is, the function returns 1, as 0!=10! = 1.
  4. Recursive Case:

    • If n is greater than 0, the function calls itself with the argument n - 1 and multiplies the result by n. This continues until the base case is reached.
  5. Main Function:

    • The program starts by prompting the user to enter a non-negative integer.
    • It uses scanf() to read the input value.
  6. Negative Input Check:

    • Before calculating the factorial, the program checks if the input number is negative. If it is, it informs the user that the factorial is not defined for negative numbers.
  7. Calculating Factorial:

    • If the input is valid (non-negative), the program calls the factorial() function and stores the result.
    • It then prints the factorial of the entered number.

How to Run the Program

  1. Compile the Code: Use a C compiler like gcc to compile the code.

    gcc factorial_recursive.c -o factorial_recursive
  2. Execute the Program:

    ./factorial_recursive
  3. Input Data: Enter a non-negative integer when prompted.

Example Input/Output

Input:

Enter a non-negative integer: 5

Output:

Factorial of 5 is 120

Conclusion

The "Factorial Using Recursion" program in C demonstrates the power of recursion in simplifying complex problems. By breaking down the problem into smaller sub-problems, it provides a clear and elegant solution for calculating the factorial of a number. This program is a good example of how recursive functions can be implemented and used effectively in C programming.