Calculating n! with a loop You have been provided with a template for a program
ID: 3686147 • Letter: C
Question
Calculating n! with a loop You have been provided with a template for a program that will calculate factorials for the numbers 1 through 20. The values for 1! to 12! can fit into an unsigned long int (assuming it is a 4 byte integer). For us to support up to 20! we need to use a long long int or an unsigned long long int Either will work. Do not use float or double or any other floating point type. The float types do not have enough digits of precision for 20! have a loop in your main function, a loop in getNumber and a loop in calcula Use a for one, a do while loop for another, and a for loop for the other. You can decide which loop to use in each of the three functions, but you need to use a different type of loop in each of the functions. main function The main function will have a processing loop. Your program willread in a number using the getNumber function. The getNumber function will retum a value between 0 and 20, inclusive. If the number is greater than 0 your main function will call the calculateFactorial function. The calculateFactorial function will retum the factorial for the number you received from getNumber (you pass that number to calculateFactorial). Your main function then needs to output the value retumed from calculateFactorial. If the number you received from getNumber is not 0 you need to process the loop again (to read in and calculate a factorial). If the number was 0 you exit the loop and your program will end. getNumber function The getNumber function will ask the use to enter in a number. A valid number is one that is greater than or equal to 0, and is less than or equal to 20. If the number is not valid you must ask the user to enter in a valid number. You will need to continue to ask the user for a valid number until they enter a value between 0 and 20, inclusive. You function should then retum the valid number entered by the user calculateFactorial function: This is where you will calculate the factorial. You are passed in a number N and then you calculate Ni. Your function should then retum the value for NI. Your function needs to use a long long int or unsigned long long int for this since an unsigned long int will not be large enough for all of the factorials between 1 and 20.Explanation / Answer
#include <iostream>
using namespace std;
int getNumber();
unsigned long long int calculateFactorial(int value);
int main() {
int num = getNumber();
unsigned long long int fact = calculateFactorial(num);
cout << num <<"! = " << fact <<endl;
return 0;
}
int getNumber() {
int num = 0;
do {
cout << (" Enter a number in range [0,20]") << endl;
cin >> num;
} while(num < 0 || num > 20);
return num;
}
unsigned long long int calculateFactorial(int value) {
unsigned long long int fact = 1;
int i = 1;
for(i = 1; i <= value; i++)
fact *= i;
return fact;
}
---output----------------------
Enter a number in range [0,20]
34
Enter a number in range [0,20]
-1
Enter a number in range [0,20]
19
19! = 121645100408832000
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.