Answered! Using recursion write an algorithm to find the value of n! for given natural number n….

Using recursion write an algorithm to find the value of n! for given natural number n.

Implement the algorithm in a C++ program.

Expert Answer

 Algorithm:
Step 1: Start
Step 2: Read number n
Step 3: Call factorial(n)
Step 4: Print factorial f
Step 5: Stop

factorial(n)
Step 1: If n>1 then  
        f=n*factorial(n-1)
Step 2: Return f

step 3: else return 1

Program :

#include<iostream>
using namespace std;

int factorial(int n);       // Function prototype

int main()
{
    int n;

    cout << "Enter a positive integer: ";
    cin >> n;

    cout << "Factorial of " << n << " = " << factorial(n);

    return 0;
}

int factorial(int n)               // Recursive call
{
    if(n > 1)
        return n * factorial(n - 1);
    else
        return 1;
}

output :

Still stressed from student homework?
Get quality assistance from academic writers!