Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The Problem Statement You are creating a program to help you understand some important math concepts: factorials and exponents. Since you are focused on understanding

The Problem Statement

You are creating a program to help you understand some important math concepts: factorials and exponents.

Since you are focused on understanding the concepts, you will not use the math library (math.h).

Program Setup A scaffold of the solution has been created for you:

#include

//function prototypes int exp(int base, int exponent); /***Add the factorial function prototype here***/

//main function int main(){ int n, pow, result, option;

printf("What would you like to do? "); printf("1 - Calculate a power. "); printf("2 - Calculate a factorial. "); scanf("%d", &option);

while (option != 1 && option != 2) { printf("Sorry, that was not a valid option. "); printf("What would you like to do? "); printf("1 - Calculate a power. "); printf("2 - Calculate a factorial. "); scanf("%d", &option); }

if (option == 1) { printf("Please enter the base value. "); scanf("%d", &n);

printf("Please enter the exponent. "); scanf("%d", &pow);

while (pow < 0) { printf("Sorry, that was not a valid exponent. "); printf("Please enter the exponent. "); scanf("%d", &pow); }

/***Add the function call for exp here***/

printf("The result of %d raised to %d is %d. ", n, pow, result); } else { printf("Please enter a value. "); scanf("%d", &n);

while (n <= 0 || n >= 13) { printf("Sorry, that was not a valid number. "); printf("Please enter a value. "); scanf("%d", &n); }

result = fact(n);

printf("%d! = %d.", n, result); }

return 0; }

// Precondition: base is a real number, exp >= 0 // Postcondition: Returns base raised to the exp power. int exp(int base, int exponent) { int i, result = 1;

for(i=0; i

return result; }

/*** Add the factorial function here ***/

Do not modify the code that is already present. Instead, fill in the sections that are marked like this: /*** ... ***/

One of those sections will be the function to calculate a factorial. Your function will take in a single non-negative integer (n) and you'll calculate n! or n factorial. To calculate n! you would calculate the following product: 1x2x3x...xn. Function Prototype You must use this prototype to receive credit for the assignment. // Pre-condition: 0 < n < 13 // Post-condition: Returns n! int fact(int n);

Sample Runs What would you like to do?

1 - Calculate a power.

2 - Calculate a factorial 2 Please enter a value. 7 7! = 5040. ---

What would you like to do?

1 - Calculate a power.

2 - Calculate a factorial 1 Please enter the base value. 2 Please enter the exponent. 3 The result of 2 raised to 3 is 8.

Do not modify any of the printf statements in the scaffold. Do not add any printf statements to the program.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL Server Query Performance Tuning

Authors: Sajal Dam, Grant Fritchey

4th Edition

1430267429, 9781430267423

More Books

Students also viewed these Databases questions

Question

What would you do?

Answered: 1 week ago

Question

2. How much time should be allocated to the focus group?

Answered: 1 week ago

Question

1. Where will you recommend that she hold the focus group?

Answered: 1 week ago