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;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.