c++ program to find factorial of a number

to find factorial
of
a number

The calculation of factorial in programming is also an example of
recursion.
But the recursion isn't memory efficient if you don't care about this, It is a choice to save
time.

The factorial of any number n > 1 is n multiplied by the factorial of n - 1. See the
implementation.

Example

fact(4)

4 * fact(3)

4 * 3 * fact(2)

4 * 3 * 2 * fact(1)

The factorial of a non-negative integer n is equal to
the
product of
all positive integers from 1 to n inclusively. Also by definition,
the factorial of 0 is 1.

Code snippet: C++ program to find factorial of n

    #include <iostream >
    using namespace std;

    // program to calculate factorial of a num ->>>>
    int factorial(int n)
    {
    if (!(n > 1 ) || n == 0)
    return 1;
    else
    return n * factorial(n-1);
    }

    int main() {

    cout << "Enter the value of N : ->> " << endl; int N; cin>> N;

        cout<< "factorial of N is : ->> " << factorial(N); return 0; }
    

Share this Post

Leave a Reply

Your email address will not be published. Required fields are marked *