Question: language: c++ Programming Fundamentals CS 1336 Fall 2017 Homework Assignment #5 Calculate Factorials Calculate the factorials for the numbers 1 through 20. The values for

language: c++

Programming Fundamentals CS 1336 Fall 2017 Homework Assignment #5 Calculate Factorials

Calculate the 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!.

You will have a loop in your main function, a loop in getNumber and a loop in calculateFactorial. Use a while loop for one, a do while loop for another, and a for loop for the third. 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 will read in a number using the getNumber function. The getNumber function will return 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 return the factorial for the number you received from getNumber (you pass that number to calculateFactorial). Your main function then needs to output the value returned 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 return 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 N!. Your function should then return the value for N!. 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.

Here is sample output from the program: Enter 0 to quit or a number n to be used for n! up to 20!: 1[Enter]

1! is 1 Enter 0 to quit or a number n to be used for n! up to 20!: -1[Enter] The value must be greater than or equal to 0 and less than 21. Enter 0 to quit or a number n to be used for n! up to 20!: 2[Enter]

2! is 2 Enter 0 to quit or a number n to be used for n! up to 20!: 21[Enter] The value must be greater than or equal to 0 and less than 21. Enter 0 to quit or a number n to be used for n! up to 20!: 4[Enter] 4! is 24 Enter 0 to quit or a number n to be used for n! up to 20!: 8[Enter] 8! is 40320 Enter 0 to quit or a number n to be used for n! up to 20!: 5[Enter] 5! is 120 Enter 0 to quit or a number n to be used for n! up to 20!: 12[Enter] 12! is 479001600 Enter 0 to quit or a number n to be used for n! up to 20!: 13[Enter] 13! is 6227020800 Enter 0 to quit or a number n to be used for n! up to 20!: 20[Enter] 20! is 2432902008176640000 Enter 0 to quit or a number n to be used for n! up to 20!: 0[Enter]

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!