< previous page page_251 next page >

Page 251
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
with two digits after the decimal point indicating the radius of the particular circle.
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
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
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
A 6.75
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
your program should print something like this:
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
The area of a circle with radius 6.75 is 143.14.
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
Here are the formulas you'll need:
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
Diameter = 2r
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
Circumference = 2pr
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
Area of a circle = pr2
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
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:
0251-01.gif
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
where p=3.14159265 and e=2.718282.
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
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.
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
Depending on the value ofn, you should obtain one of these results:
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
A numerical result.
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
If n equals 0, the factorial is defined to be 1.
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
If n is less than 0, the factorial is undefined.
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
If n is too large, the result exceeds LONG_MAX.
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
(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.)
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
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..

 
< previous page page_251 next page >