C Program to Calculate the Factorial of a Number Using Recursion - Source Code, Output & Execution Flow

C Program to Calculate the Factorial of a Number Using Recursion

This C program calculates factorial using a recursive function.

#include <stdio.h>
long long factorial(int n) {
    if (n == 0 || n == 1) return 1;
    return n * factorial(n - 1);
}
int main() {
    int n;
    printf("Enter a number: ");
    scanf("%d", &n);
    printf("Factorial = %lld
", factorial(n));
    return 0;
}
Enter a number: 5
Factorial = 120

In this program, the input is read first when required, the core logic is applied step by step, and the final result is displayed using printf(). Follow the execution flow beside the code to understand how each statement contributes to the output.

Sai Kishore Randhi, author
About the author

Sai Kishore Randhi

Embedded systems, VLSI, and chip design

Sai Kishore writes about embedded systems, VLSI, chip design, and the programming concepts behind modern hardware.

View LinkedIn profile ↗