Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Show that the factorial F(n) of a positive integer number n, i. e. , F(n) = I. 2

ID: 674141 • Letter: S

Question

Show that the factorial F(n) of a positive integer number n, i. e. , F(n) = I. 2. 3. 4. . (n-l). n may be expressed as a recursive function. Then design a program in C using the method of call by value and the recursive property of F(n) which, upon running, would generate the value of F(n) for a user-specified value of n. Make sure to include suitable "printf" statements so that, when the computation of F(n) is performed and outputted to the console, the output shows all the intermediate computational steps prior to displaying the computed value of F(n). Also you are to include a test to make sure that the integer number n you type is positive, i. e. , the program should not let you go forward until you type a positive integer number.

Explanation / Answer

#include<iostream>

#include<string.h>

using namespace std;

int factorial(int n) {

if(n==1) {

cout<<" Recursion complete. Calculating product in reverse ";

return 1;

}

  

cout<<"Recursion with n value:"<<n<<" ";

int fac = n*factorial(n-1);

cout<<"Intermediate product: "<<fac<<" ";

return fac;

}

int main() {

  

int in;

  

while(true) {

cout<<"Enter a positive number: ";

cin>>in;

  

if(in <= 0)

cout<<"Please enter valid number. ";

else

break;

}

cout<<" Intermediate steps : ";

int fac = factorial(in);

cout<<" Factorial of "<<in<<" is : "<<fac<<" ";

  

  

cout<<" ";

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote