Answer to Question #330706 in C++ for Secret Agent

Question #330706

And That's a Fact!

by CodeChum Admin

I love facts. In fact, I dream a lot about facts. Ever since I was a kid, all I ever think of are facts. And that's a fact!


I don't know if that made any sense to you but I think you know about factorials, hmm? In Mathematics, a factorial of a number is the product of all the numbers from 1 up to that number. There is one exception though and that is the factorial of 0 which is 1.


Instructions:

  1. In the code editor, you are provided with a main function that asks the user for an integer input and passes this value to a function called, getFactorial()
  2. The getFactorial() function has the following description:
  3. Return type - int
  4. Name - getFactorial
  5. Parameters - one integer
  6. Description - returns the factorial of the passed integer
  7. Your implementation should be RECURSIVE and you should not use any loops

Input


1. Integer to be processed

Output


Enter·n:·3
Factorial·of·3·is·6




1
Expert's answer
2022-04-20T15:35:07-0400
#include <iostream>
using namespace std;


int getFactorial(int n) {
    if (n <= 1) {
        return 1;
    }
    return n*getFactorial(n-1);
}


int main() {
    int n;
    cout << "ENter n: ";
    cin >> n;


    int fact = getFactorial(n);
    cout <<"Ractorial of " << n << " is " << fact << endl;


    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS