CODE USING C++ 3. Outsmarting the Guard by CodeChum Admin Great! Thanks to your help I've now reached the enemy's base. According to the plan, no one's supposedly going to see me but this guard has...





CODE USING C++


3. Outsmarting the Guard


by CodeChum Admin






Great! Thanks to your help I've now reached the enemy's base.



According to the plan, no one's supposedly going to see me but this guard has outsmarted me! He's big and mean and he's in here in front of me!



For some reason though, although he looks scary on the outside, he's soft in the inside. He agreed to let me pass as long as I could compute the factorial of certain number. Please help me, Programmer!





Instructions:



  1. In the code editor, you are given the main() code with the initial version of the factorial() function.

  2. To recap your basic math, the factorial of a number is basically the product of all the numbers from 1 up to that number. For example, the factorial of 5 is 120 because 1 x 2 x 3 x 4 x 5 = 120 and factorial of 3 is just 6 because 1 x 2 x 3 = 6. However, there's a special case and that is the factorial of 0 which is 1.

  3. The initial factorial() function lacks the recursive case. Fix it so the guard would let Agent J. in.






Input




1. Value of n







Output




Enter n:·5

Factorial of 5 is 120



main.cpp<br>< > +<br>C++<br>Tests<br>1 #include <iostream><br>2 using namespace std;<br>CE Run Tests<br>3<br>4 int factorial(int);<br>Constraints<br>6 int main(void) {<br>int n;<br>7<br>8<br>O Constraint 1<br>cout << > n; 11 12 cout <« "factorial="" of="" "=""><« n="" «="" "="" is="" "="" «="" factorial(n);="" 13="" o="" constraint="" 2="" 14="" return="" 0;="" the="" 'factorial="" function="" should="" apply="" recursion.="" 15="" }="" 16="" 17="" int="" factorial(int="" n)="" {="" if(n=""><= 1)="" {="" return="" 1;="" test="" cases="" 18="" 19="" }="" else="" {="" todo:="" add="" the="" recursive="" case="" here="" o="" test="" case="" 1="" 20="" 21="" ...="" 22="" your="" output="" return="" }="" 24="" }="" 23="" no="" output="" expected="" output="" enter="" n:="" 5="" factorial="" of="" 5="" is="" 120="" o="" test="" case="" 2="" your="" output="" execute="" code="" [f10]="" submit="" code="" 1="" launch="" me="" "/="">
Extracted text: main.cpp <> + C++ Tests 1 #include 2 using namespace std; CE Run Tests 3 4 int factorial(int); Constraints 6 int main(void) { int n; 7 8 O Constraint 1 cout < "enter="" n:="" ";="" the="" code="" should="" call="" 'factorial'="" in="" main.="" 10="" cin="">> n; 11 12 cout <« "factorial="" of="" "=""><« n="" «="" "="" is="" "="" «="" factorial(n);="" 13="" o="" constraint="" 2="" 14="" return="" 0;="" the="" 'factorial="" function="" should="" apply="" recursion.="" 15="" }="" 16="" 17="" int="" factorial(int="" n)="" {="" if(n=""><= 1) { return 1; test cases 18 19 } else { // todo: add the recursive case here o test case 1 20 21 ... 22 your output return } 24 } 23 no output expected output enter n: 5 factorial of 5 is 120 o test case 2 your output execute code [f10] submit code 1 launch me 1)="" {="" return="" 1;="" test="" cases="" 18="" 19="" }="" else="" {="" todo:="" add="" the="" recursive="" case="" here="" o="" test="" case="" 1="" 20="" 21="" ...="" 22="" your="" output="" return="" }="" 24="" }="" 23="" no="" output="" expected="" output="" enter="" n:="" 5="" factorial="" of="" 5="" is="" 120="" o="" test="" case="" 2="" your="" output="" execute="" code="" [f10]="" submit="" code="" 1="" launch="">
Jun 06, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here