C PROGRAM ONLY) 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

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

(C PROGRAM ONLY)

 

 

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.c
> + C
3 int factorial(int);
4
5 int main(void) {
6
int n;
7
8
printf("Enter n: ");
9
scanf("%d", &n);
10
11
printf("Factorial of %d is %d", n, factorial(n));
12
13
return 0;
14}
15
16 int factorial(int n) {
17
if(n <= 1) {
18
return 1;
19
} else {
20
// TODO: Add the recursive case here
21
return
22
}
Transcribed Image Text:main.c > + C 3 int factorial(int); 4 5 int main(void) { 6 int n; 7 8 printf("Enter n: "); 9 scanf("%d", &n); 10 11 printf("Factorial of %d is %d", n, factorial(n)); 12 13 return 0; 14} 15 16 int factorial(int n) { 17 if(n <= 1) { 18 return 1; 19 } else { 20 // TODO: Add the recursive case here 21 return 22 }
Expert Solution
steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Structure chart
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education