Table of Contents
You will practice using what you’ve previously learned in your programming classes to get comfortable working in C again.
You will use common programming principles, such as variables, constants, branching, loops and functions to complete this assignment.
Write and compile code online at code.bilimedtech.com!
You will create a C program that displays the factorial of a number.
The factorial of a number is the product of all positive integers less than or equal to n:
For example, 5! is 5 * 4 * 3 * 2 * 1 = 120
You will create a simple program that calculates the factorial of a given number.
Start with the command line template from C Templates.
Wait till the next step to set the value from the command arg.
Focus on developing the algorithm before adding complexity.
Create a function with this prototype:
Print the result in
It is not common to prompt users for input when writing low-level code. Instead, data is passed into the program through parameters or arguments.
The program should accept a single integer parameter.
Check the number of args. If the arg is missing, generate the following message and exit gracefully.
usage: factorial integer
Remember to use function
atoi(string)to convert the input arg to an integer.
The program works but is error-prone and does not use resources
efficiently. The return type based on the function prototype of
int get_factorial(int); is not appropriate because a factorial
value must be a positive integer. The default data types in C are
signed. Furthermore, entering an integer that is too large or a
negative value will produce inaccurate results.
./factorial 86 86! is: 0 ./factorial 29 29! is: -1241513984 ./factorial -8 -8! is: 1
Use an online factorial calculator to determine the result of 20!.
Try calculating the factorial of 20 in your program.
What value did you get? Was it negative? If so, what could cause that?
Determine the maximum integer value used to calculate the factorial for types:
short: _____________ unsigned short: _____________ int: _____________ unsigned int: _____________ long: _____________ unsigned long _____________
We should use the appropriate data types for the size of our values.
Chose an unsigned C data type that you want to use for the factorial result.
Change the data type in the function prototype to use that type.
Verify that your program produces the correct results up to the maximum value.
// Prototype char get_factorial(int); // Output 5! is: 120 6! is: -48
We should prevent invalid input by checking the size before performing the calculation. It is not enough to add error handling before calling the function. We need to add error handling inside of the function. We can’t trust that the caller will check the input value.
Determine the largest integer value that a user can enter for your chosen data type. For example, a type
charwill hold the correct results up to 5! (120). 6! is 720, which exceeds an 8-bit value.
Create a global
constof the type above
main()that contains the maximum input value for your data type.
Type the name of the
constvariable in ALL_CAPS to follow the common C style guide.
get_factorial()if the value is too large or a negative value.
constinstead of hard-coding the integer value.
Generate an error message if the user enters a value that is too big. Inform the user of the largest value possible for your program.