Table of contents
1.
Introduction
2.
What is the Factorial Program in C?
3.
Factorial Program in C using Recursion
3.1.
Using Tail Recursion
4.
Frequently Asked Questions
4.1.
What is factorial program in C?
4.2.
How to write factorial formula in C?
4.3.
What is the factorial of 100 factorial?
5.
Conclusion
Last Updated: Jun 25, 2024
Easy

Factorial using Recursion in C

Author Ravi Khorwal
0 upvote

Introduction

Calculating factorials using recursion in C is a fundamental programming concept that showcases the power of functions calling themselves. In this article, we'll explore how to implement a recursive function to find the factorial of a number. 

Factorial using recursion in C

What is the Factorial Program in C?

The factorial of a non-negative integer n is denoted as n! and is defined as the product of all positive integers less than or equal to n. The factorial function can be formally defined as:

n! = n * (n-1) * (n-2) * ... * 1 for n > 0

0! = 1

A C program that computes the factorial of a given number is known as a factorial programme. In C, there are two primary approaches to writing a factorial programme either using a loop or using recursion. Today, we will read about how to do this using recursion.

Factorial Program in C using Recursion

A recursive approach to finding the factorial involves calling the function itself with a reduced problem size. Here's the code:

#include <stdio.h>

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

int main() {
    unsigned int n;
    printf("Enter a number: ");
    scanf("%u", &n);
    printf("Factorial of %u is %llu\n", n, factorial(n));
    return 0;
}
You can also try this code with Online C Compiler
Run Code

Using Tail Recursion

Tail recursion optimizes recursive functions by reducing the amount of memory used. Here's the factorial code with tail recursion:

#include <stdio.h>

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

unsigned long long factorial(unsigned int n) {
    return factorial_helper(n, 1);
}

int main() {
    unsigned int n;
    printf("Enter a number: ");
    scanf("%u", &n);
    printf("Factorial of %u is %llu\n", n, factorial(n));
    return 0;
}
You can also try this code with Online C Compiler
Run Code

Frequently Asked Questions

What is factorial program in C?

The factorial of a non-negative integer is calculated via a factorial programme written in C. All positive integers up to the specified number are added together to form it. For instance, 120 is the factorial of 5 (5!).

How to write factorial formula in C?

Apply a loop or recursion to create a factorial formula in C. An example of a recursive method is factorial(n) = (n == 0) ? 1 : n * factorial(n - 1). For instance, 120 is the factorial of 5 (5!).

What is the factorial of 100 factorial?

The sum of all positive integers less than or equal to 100 factorial is known as the factorial of 100 factorial. This number is roughly equal to 9.3326215443944e+157, although it cannot be printed out in full due to its size.

Conclusion

Understanding how to calculate factorials using recursion in C is a valuable skill that highlights the elegance and efficiency of recursive functions. By mastering this technique, you enhance your problem-solving toolkit, making it easier to tackle more complex programming challenges. Keep practicing, and you'll see just how powerful recursion can be.

Recommended Reading:

  1. Decimal to Binary in C
  2. Ternary Operator in C
  3. Conditional Operator in C
  4. Prime Number Program using C

 

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass