Question
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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started