ODE 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: In the code editor, you are given the main() code with the initial version of the factorial() function. 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. 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

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

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
< > +
C++
Tests
1 #include <iostream>
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
Transcribed Image Text:main.cpp < > + C++ Tests 1 #include <iostream> 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
Expert Solution
steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY