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

A factorial of a n=n*(n-1)*(n-2)*...*3*2*1. Forexample, 5!(fivefactorial) is 5*4

ID: 3806655 • Letter: A

Question

A factorial of a n=n*(n-1)*(n-2)*...*3*2*1. Forexample, 5!(fivefactorial) is 5*4*3*2*1=120.

Individually write (using pencil/pen and paper) a recursive function factorial that will take an integer value n as an argument, and will return n! . Then compare your function to your partner’s, together (i) come up with a function implementation you both agree on, and (ii) write it as a C++ function on the computer. Then write a short C++ program that will call your function and display the factorial of 10.

Explanation / Answer

#include <iostream>
using namespace std;
int fact(int n)
{
   if(n<=1)
   {
       return 1;
   }
   return n*fact(n-1);
}
int main()
{
   cout<<fact(10)<<endl;
}

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