CPP Program to find the factorial by recursion

 In this program, the solution of finding the factorial of any number positive number by using the recursion method in the cpp language. For a complete understanding of this code, you must have knowledge of the cpp recursion.

Code:

#include<iostream>

using namespace std;


int fact(int n); //declare the function


int main()

{

    int n;


    cout << "Enter a positive integer for finding the factorial ";

    cin >> n;


    cout << "Factorial of the number " << n << "= " << fact(n);


    return 0;

}


int fact(int n) //define the function for factorial

{

    if(n >= 1)

        return n * fact(n - 1);

    else

        return 1;

}


Output:
Enter a positive integer for finding the factorial 4
Factorial of the number 4= 24

Comments

Popular posts from this blog

Diferrence between flutter and react native

Flutter Widgets

Flutter layout