CHAPTER 4 SOLUTIONS

Q1. Write a program that prompts the user to input a number. The program
should then output the number and a message saying whether the number is
positive, negative, or zero.

Q2.Write a program that prompts the user to input three numbers. The
program should then output the numbers in ascending order.

Q3.Write a program that prompts the user to input an integer between 0 and 35 . If the number is less than or equal to 9 , the program should output the number;
otherwise, it should output A for 10 , B for 11 , C for 12 . . . and Z for 35 . (Hint:
Use the cast operator, static_cast<char( ) , for numbers = 10 .)

Q4.The statements in the following program are in incorrect order. Rearrange
the statements so that they prompt the user to input the shape type
( rectangle , circle , or cylinder ) and the appropriate dimension of
the shape. The program then outputs the following information about the
shape: For a rectangle, it outputs the area and perimeter; for a circle, it
outputs the area and circumference; and for a cylinder, it outputs the
volume and surface area. After rearranging the statements, your program
should be properly indented.

Q5.Suppose that the cost of sending an international fax is calculated as follows:
The service charge is \$ 3.00, \$ .20 per page for the first 10 pages, and \$ 0.10 for
each additional page. Design an algorithm that asks the user to enter the
number of pages to be faxed. The algorithm then uses the number of pages
to be faxed to calculate the amount due.
Write a program to implement the algorithm you designed in Exercise 21 of
Chapter 1.

Q6.In a right triangle, the square of the length of one side is equal to the sum
of the squares of the lengths of the other two sides. Write a program that
prompts the user to enter the lengths of three sides of a triangle and then
outputs a message indicating whether the triangle is a right triangle.

Q7.A box of cookies can hold 24 cookies, and a container can hold 75 boxes
of cookies. Write a program that prompts the user to enter the total
number of cookies, the number of cookies in a box, and the number of
cookie boxes in a container. The program then outputs the number of
boxes and the number of containers to ship the cookies. Note that each
box must contain the specified number of cookies, and each container
must contain the specified number of boxes. If the last box of cookies
contains less than the number of specified cookies, you can discard it and
output the number of leftover cookies. Similarly, if the last container
contains less than the number of specified boxes, you can discard it and
output the number of leftover boxes.

Q8.The roots of the quadratic equation ax 2 + bx + c = 0, a != 0 are given by the
following formula:
-b +- sqt(b^2 -4ac)/2a

In this formula, the term b^2 - 4ac is called the discriminant. If b^2 - 4ac = 0,
then the equation has a single (repeated) root. If b^2 - 4ac 0, the
equation has two real roots. If b^2 - 4ac < 0, the equation has two
complex roots. Write a program that prompts the user to input the
value of a (the coefficient of x 2 ), b (the coefficient of x), and c (the
constant term) and outputs the type of roots of the equation. Further-
more, if b 2 À 4ac ! 0, the program should output the roots of the
cmath to calculate the square root. Chapter 3 explains how the func-
tion pow is used.)

Q9.Write a program that mimics a calculator. The program should take as input
two integers and the operation to be performed. It should then output the
numbers, the operator, and the result. (For division, if the denominator is
zero, output an appropriate message.) Some sample outputs follow:
3 + 4 = 7
13 * 5 = 65

Q10. Write a program that mimics a calculator. The program should take as input
two integers and the operation to be performed. It should then output the
numbers, the operator, and the result. (For division, if the denominator is
zero, output an appropriate message.) Some sample outputs follow:
3 + 4 = 7
13 * 5 = 65

Q11. You found an exciting summer job for five weeks. It pays, say, \$15.50
per hour. Suppose that the total tax you pay on your summer job
income is 14%. After paying the taxes, you spend 10% of your net
income to buy new clothes and other accessories for the next school
year and 1% to buy school supplies. After buying clothes and school
supplies, you use 25% of the remaining money to buy savings bonds.
For each dollar you spend to buy savings bonds, your parents spend
\$0.50 to buy additional savings bonds for you. Write a program that
prompts the user to enter the pay rate for an hour and the number
of hours you worked each week. The program then outputs the
following: