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:

An algorithm is astep by step procedure to solve a given problem.
step1:
initilizes the n value to zero first
step 2:
enter the n value
step 3:
store the n value in a declared variable
step 4:
now pass the n value the required funtion
step 5:
calculate the result if n is positive value
step 6:
repeat the above step till n value becomes to 1 and store the result in a variable if required
step 7:
display the result.

Example program:

program for to find the value of n! for given natural number n

i execute the program and show the sample output for u.

#include<iostream>
using namespace std;

int fact(int n);

int main()
{
int n;

cout << “enter a positive integer: “;
cin >> n;

cout << “factorial of ” << n << ” = ” << fact(n);

return 0;
}

int fact(int n)
{
if(n > 1)
return n * fact(n – 1);
else
return 1;
}

output:

Enter a positive integer: 10

Factorial of 10 = 3628800

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