Professional Documents
Culture Documents
Page1
6. Write a weather-calculator program that asks for a list of the previous 10 days temperatures,
computes the average, and prints the results. You have to compute the total as the input occurs, then
divide that total by 10 to find the average. Use a while loop for the 10 repetitions.
LoopsPracticequestions
Page2
7. Write a program that produces your own ASCII table onscreen. Dont print the first 31 characters
because they are nonprintable. Print the codes numbered 32 through 255 by storing their numbers in
integer variables and printing their ASCII values.
8. Drivers are concerned with the mileage obtained by their automobiles. One driver has kept
track of several tankfuls of gasoline by recording miles driven and gallons used for each
tankful. Develop a C++program that uses a whi l e statement to input the miles driven and
gallons used for each tankful. The program should calculate and display the miles per gallon
obtained for each tankful and print the combined miles per gallon obtained for all tankfuls up
to this point.
Ent er t he mi l es used ( - 1 t o qui t ) : 287
Ent er gal l ons: 13
MPG t hi s t ankf ul : 22. 076923
Tot al MPG: 22. 076923
Ent er t he mi l es used ( - 1 t o qui t ) : 200
Ent er gal l ons: 10
MPG t hi s t ankf ul : 20. 000000
Tot al MPG: 21. 173913
Ent er t he mi l es used ( - 1 t o qui t ) : 120
Ent er gal l ons: 5
MPG t hi s t ankf ul : 24. 000000
Tot al MPG: 21. 678571
Ent er mi l es ( - 1 t o qui t ) : - 1
9. One large chemical company pays its salespeople on a commission basis. The salespeople
each receive $200 per week plus 9 percent of their gross sales for that week. For example, a
salesperson who sells $5000 worth of chemicals in a week receives $200 plus 9 percent of
$5000, or a total of $650. Develop a C++program that uses a whi l e statement to input each
salesperson's gross sales for last week and calculates and displays that salesperson's earnings.
Process one salesperson's figures at a time.
Ent er sal es i n dol l ar s ( - 1 t o end) : 5000. 00
Sal ar y i s: $650. 00
Ent er sal es i n dol l ar s ( - 1 t o end) : 6000. 00
Sal ar y i s: $740. 00
Ent er sal es i n dol l ar s ( - 1 t o end) : 7000. 00
Sal ar y i s: $830. 00
Ent er sal es i n dol l ar s ( - 1 t o end) : - 1
LoopsPracticequestions
Page3
10. The process of finding the largest number (i.e., the maximum of a group of numbers) is used
frequently in computer applications. For example, a program that determines the winner of a
sales contest inputs the number of units sold by each salesperson. The salesperson who sells
the most units wins the contest. Write a pseudocode program, then a C++program that uses a
whi l e statement to determine and print the largest number of 10 numbers input by the user.
Your program should use three variables, as follows:
count er : A counter to count to 10 (i.e., to keep track of how many numbers have been input
and to determine when all 10 numbers have been processed).
number : The current number input to the program.
l ar gest : The largest number found so far.
11. Find two largest values among the 10 numbers. [Note: You must input each number only
once.]
12. A palindrome is a number or a text phrase that reads the same backwards as forwards. For
example, each of the following five-digit integers is a palindrome: 12321, 55555, 45554 and
11611. Write a program that reads in a five-digit integer and determines whether it is a
palindrome. [Hint: Use the division and modulus operators to separate the number into its
individual digits.]
13. Write a program that uses a f or statement to sum a sequence of integers. Assume that the
first integer read specifies the number of values remaining to be entered. Your program
should read only one value per input statement. A typical input sequence might be
5 100 200 300 400 500
where the 5 indicates that the subsequent 5 values are to be summed.
14. Write a program that uses f or statements to print the following patterns separately, one
below the other. Use f or loops to generate the patterns. All asterisks (*) should be printed by
a single statement of the form cout << ' *' ; (this causes the asterisks to print side by side).
[Hint: The last two patterns require that each line begin with an appropriate number of
blanks. Extra credit: Combine your code from the four separate problems into a single
program that prints all four patterns side by side by making clever use of nested f or loops.]
( a) ( b) ( c) ( d)
* ********** ********** *
** ********* ********* **
*** ******** ******** ***
**** ******* ******* ****
***** ****** ****** *****
****** ***** ***** ******
******* **** **** *******
******** *** *** ********
********* ** ** *********
********** * * **********
LoopsPracticequestions
Page4
15. A mail order house sells five different products whose retail prices are: product 1 $2.98,
product 2$4.50, product 3$9.98, product 4$4.49 and product 5$6.87. Write a program that
reads a series of pairs of numbers as follows:
a. product number
b. quantity sold
Your program should use a swi t ch statement to determine the retail price for each product.
Your program should calculate and display the total retail value of all products sold. Use a
sentinel-controlled loop to determine when the program should stop looping and display the
final results.
16. Write a program that prints the following diamond shape. You may use output statements
that print either a single asterisk (*) or a single blank. Maximize your use of repetition (with
nested f or statements) and minimize the number of output statements.
*
***
*****
*******
*********
*******
*****
***
*
17. Write a program which has the following output screen. (use loops to control output of the
program)
LoopsPracticequestions
Page5
18. ProblemDescription
Write a program which has the following output screen. (use loops to control output of the
program)