Skip to main content

Factorial Using Recursion Example Program In C++

1 min read Updated June 30, 2026
Share:
On this page (6sections)

About this program

This is an example program in c common example program. Read the concept first: C++ Variables and Data Types, then study the code and output below.

Definition

In mathematics, the factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n. For example,

6! = 6 x 5 x 4 x 3 x 2 x 1 = 720

The value of 0! is 1, according to the convention for an empty product.

Factorial Using Recursion Example Program

/*  Example Program For Factorial Value Using Recursion In C++
    little drops @ thiyagaraaj.com

    Coded By:THIYAGARAAJ MP             */

#include<iostream>
#include<conio.h>

using namespace std;

//Function
long factorial(int);

int main() {

    // Variable Declaration
    int counter, n;

    // Get Input Value
    cout << "Enter the Number :";
    cin>>n;

    // Factorial Function Call
    cout << n << " Factorial Value Is " << factorial(n);

    // Wait For Output Screen
    getch();
    return 0;
}

// Factorial recursion Function

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

Sample Output

Enter the Number :6
6 Factorial Value Is 720

Learn the concept first, then study the code:

Frequently Asked Questions

What does this C++ program do?
It is a C++ example program that demonstrates Factorial Using Recursion, including the complete source code and the expected sample output.
How do I compile and run this C++ program?
Save the code in a `.cpp` file, compile it with `g++ filename.cpp -o program`, then run it with `./program` (or `program.exe` on Windows).
What concepts does this example use?
This example uses conditional logic, reading user input and user-defined functions, illustrating a common pattern in C++ programming.

Related Tutorials

Search tutorials