ICS 101 - Computer programming

Lab # 9

(WHILE LOOP)

 

 

Objectives

 

 

Syntax

 

 DO  WHILE (Condition)

 

     Block of Statements

 

 end do

 

 

 

 

Exercise 1

 

A small ball is dropped from a height of 200 centimeters. Given that each time the ball bounces it reaches 0.75 of its previous height. Write a program which will count the number of bounces before the height of the ball is less than 0.1 centimeter.

 

Your output should be:

 

NUMBER OF BOUNCES =               27

 

 

Exercise 2

 

The rate of drilling a well is 20 meters per hour for the first hour. For each subsequent hour the depth drilled is 8 % less than in the previous hour. Write a program to tabulate the depth drilled and the depth reached at the end of each

hour as long as the depth drilled in a particular hour is greater than 9 meters. The output of your program should be:

 

       HOUR               DEPTH  DRILLED                   DEPTH  REACHED

           1                       20.000000                               20.000000

           2                       18.400000                               38.400000

           3                       16.928000                               55.328000

           4                       15.573760                               70.901760

           5                       14.327860                               85.229620

           6                        13.181630                                98.411250

           7                        12.127100                              110.538400

           8                        11.156930                              121.695300

           9                       10.264380                              131.959700

          10                        9.443228                              141.402900

 

 

 

Exercise 3

 

The Unit prices in a shop are based on the following table:

 

ITEM CODE

UNIT PRICE

1

20.00

2

50.00

3

200.00

                                   

Write a program to compute the net bill for a customer. A discount of 5 % is given if the total bill is more than 1000.00 Riyals otherwise no discount is given. Each input line consists of two integers: an item code and the number of units bought for that item. The last input line contains the sentinel: 0    0. ASSUME NO INVALID DATA IS READ.

 

The program must output:

            1. Item code, number of units, and the total price for each item bought.

            2. The discount.

            3. The total bill.

 

Use the following data:

 

            3                        6

            2                        3

            1                       15

            0                        0

 

Your output should be:

 

            CODE             UNITS                                PRICE

                3                     6                               1200.000000

                2                     3                                 150.000000

                1                     15                                300.000000

 

DISCOUNT  = 82.500000

NET BILL  = 1567.500000

 

 

 

Exercise 4   (Homework) DO this exercise and submit in webCT

 

Write a program that reads a REAL value S (1.5 <= S <= 15.0) and finds the smallest integer N such that:

 

            1  +  1 / 2  +  1 / 3  +  1 / 4  +  . . .  +  1 / N         >   S

 

Use the data: 12.0

 

Your output should be:

S =  12.000000

N = 91329