|
|
 |
|
|
|
|
with two digits after the decimal point indicating the radius of the particular circle. |
|
|
|
 |
|
|
|
|
The program should echo-print the input data. The output should be appropriately labeled and formatted to two decimal places. For example, if the input is |
|
|
|
 |
|
|
|
|
A 6.75 |
|
|
|
 |
|
|
|
|
your program should print something like this: |
|
|
|
 |
|
|
|
|
The area of a circle with radius 6.75 is 143.14. |
|
|
|
 |
|
|
|
|
Here are the formulas you'll need: |
|
|
|
 |
|
|
|
|
Diameter = 2r |
|
|
|
 |
|
|
|
|
Circumference = 2pr |
|
|
|
 |
|
|
|
|
Area of a circle = pr2 |
|
|
|
 |
|
|
|
|
where r is the radius. Use 3.14159265 for p. |
|
|
|
|
|
|
|
|
6. The factorial of a number n is n * (n-1) * (n-2) * * 2 * 1. Stirling's formula approximates the factorial for large values of n: |
|
|
|
 |
|
|
|
|
where p=3.14159265 and e=2.718282. |
|
|
|
 |
|
|
|
|
Write a C++ program that inputs an integer value (but stores it into a float variable n), calculates the factorial of n using Stirling's formula, assigns the (rounded) result to a long integer variable, and then prints the result appropriately labeled. |
|
|
|
 |
|
|
|
|
Depending on the value ofn, you should obtain one of these results: |
|
|
|
 |
|
|
|
|
A numerical result. |
|
|
|
 |
|
|
|
|
If n equals 0, the factorial is defined to be 1. |
|
|
|
 |
|
|
|
|
If n is less than 0, the factorial is undefined. |
|
|
|
 |
|
|
|
|
If n is too large, the result exceeds LONG_MAX. |
|
|
|
 |
|
|
|
|
(LONG_MAX is a constant declared in the header file limits .h. It gives the maximum long value for your particular machine and C++ compiler.) |
|
|
|
 |
|
|
|
|
Because Stirling's formula is used to calculate the factorial of very large numbers, the factorial will approach LONG_MAX quickly. If the factorial exceeds LONG_MAX, it will cause an arithmetic overflow in the computer, in which case the program will either stop running or continue with a strange-looking integer result, perhaps negative. Before you write the program, then, you first must write a small program that lets you determine, by trial and error, the largest value of n for which your computer system can compute a factorial using Stirling's formula. After you've determined this value, you can write the program using nested Ifs that print different messages depending on the value of n. If n is within the acceptable range for your computer system, write the number and the result with an appropriate message. If n is 0, write the message, The number is 0. The factorial is 1.. If the number is less than 0, write The number is less than 0. The factorial is undefined.. If the number is greater than the largest value of n for which your computer system can compute a factorial, write The number is too large.. |
|
|
|
|
|