WHILE LOOPS

 

Problem#1

 

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

 

Problem#2 (a)

 

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

 

Problem#2 (b)

 

Modify problem 2 (a) so that the output is:

 

HEIGHT =    100.000000  CM   NUMBER OF BOUNCES =           25

HEIGHT =    200.000000  CM   NUMBER OF BOUNCES =           27

HEIGHT =    300.000000  CM   NUMBER OF BOUNCES =           28

HEIGHT =    400.000000  CM   NUMBER OF BOUNCES =           29

HEIGHT =    500.000000  CM   NUMBER OF BOUNCES =           30

HEIGHT =    600.000000  CM   NUMBER OF BOUNCES =           31

 

Hint: Use a while-loop inside a do-loop. The do-loop index must not be modified inside the do-loop.

 

 

 

 

 

Problem#3

Write a program which will read the initial balance of a bank customer and output a bank statement for that customer.

 

The first data line contains the initial balance of the customer. Each of the remaining data lines, except the last, contains two integers: a code (1 for Deposit, 2 for Withdrawal) and the amount to be deposited or withdrawn. The last data line contains the sentinel: 0       0 . ASSUME THAT NO INVALID DATA IS READ and that a withdrawal is always less or equal to the current balance.

 

Your program must output:

     1. The initial balance.

     2. The code, amount, and the new balance for each deposit or withdrawal. 

 

Use the following data:

 

9000

1                      5000

2                      3000

2                      6000

1                      500

2                      4000

1                      3500

0                      0

 

Your output should be:

 

THE INITIAL BALANCE  =  9000

CODE                         AMOUNT                   BALANCE

     1                             5000                            14000

     2                             3000                            11000

     2                             6000                              5000

     1                               500                              5500

     2                             4000                              1500

     1                             3500                              5000

           

 

Problem#4

 

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

 

ITEM CODE

UNIT PRICE

1

20.00

2

50.00

3

300.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

 

 

Problem#5

 

Write a program to calculate the excess baggage charge for SAUDIA passengers. The free baggage allowance is based on the following table:

 

CLASS

CLASS CODE

FREE BAGGAGE ALLOWANCE (KILOGRAMS)

FIRST

1

40

HORIZON

2

30

GUEST

3

20

 

Excess baggage is charged at 1 % of the first class single fare per kilogram.

 

Each input line consists of three integers: a class code, a passengers baggage weight in kilograms, and the first class single fare for his flight. The last input line contains the sentinel 0   0    0 . ASSUME NO INVALID DATA IS READ.

 

The program must output:

Baggage weight, Excess bagage weight, and excess baggage charge for each passenger.

 

Use the following data:

 

3                      19                    3638

2                      52                    7348

2                      30                    1507

1                      80                    5393

3                      65                    5260

0                        0                          0

 

Your output should be:

 

            WEIGHT                     EXCESS WEIGHT                      CHARGE

                 19                                         0                                        0.0000000

                 52                                       22                                  1616.5600586

                 30                                         0                                        0.0000000

                 80                                       40                                  2157.2001953

                 65                                       45                                  2367.0002441

 

 

Problem#6

 

Each room in a student hostel can have a maximum of 2 occupants. Write a program to find the number of rooms having one occupant and the number of rooms having no occupants. The program must also output the room numbers of these rooms. If a room has no occupants the message NO OCCUPANTS should be printed besides the room number. If all the rooms are full the message NO VACANT ROOMS should be printed.

 

Each data line, except the last, contains three integers: a room number, and the Ids of its occupants. A  student ID of 0 indicates an empty slot in a room. Thus a room with no occupants will have two zero student IDs and a room with one occupant will have one zero and one non-zero student ID. The last data line contains the sentinel              -1    -1     -1.

 

Use the following data:

 

499                  0                      0

500                  0                      940004

501                  940000            940005

502                  940002            0

503                  0                      0

504                  940003            940008

-1                     -1                     -1

 

Your output should be:

           

            499      NO OCCUPANTS

            500

            502

            503      NO OCCUPANTS

            THERE ARE  2  ROOMS WITH NO OCCUPANTS

            THERE ARE  2  ROOMS EACH WITH ONE OCCUPANT

 

Problem#7

 

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

 

 

Problem#8

 

The days of the week have the following codes:

 

WEEK-DAY

CODE

MONDAY

1

TUESDAY

2

WEDNESDAY

3

THURSDAY

4

FRIDAY

5

SATURDAY

6

SUNDAY

7

 

If the INTEGER variables D, M, and Y represent the date, month, and year of a day, the code for a week-day can be found by the formula:

 

                                            3M  +  3                       Y                        Y

            K  =  D  +  2M  +   ¾¾¾¾   +  Y  +      ¾¾     +          ¾¾

                                                   5                             4                      100

 

 

            CODE  =  MOD(K, 7)  +  1

 

To use the formula, months 1 and 2 (i.e. January and February) of any year must be considered as months 13 and 14, respectively, of the previous year. For example, the day 26 / 1 / 1990 should be considered as 26 / 13 / 1989, and the day 18 / 2 / 1993 should be considered as 18 / 14 / 1992.

 

Write a program which will read several days and determine which of these days are Fridays. If there is no Friday the message: THERE IS NO FRIDAY is printed. Each data line contains three integers: the date, month, and year of a particular day. The last data line is the sentinel 0   0   0.

 

Use the following data:

26        2          1988

19        12        1989

14        7          1990

8          3          1991

27        6          1993

21        1          1994

0          0          0

 

Your output should be:

26        2          1988

  8        3          1991

21        1          1994

 

Problem#9

 

Write a program to print the partial sums of the series:

 

1 / 2   +   1 / 4   +   1 / 8   +   1 / 16   +   .  .  . +   1 / N   +  .  . .

 

Your  program should terminate when  1 / N  £  0.1E-9

 

Your output should be:

 

    5.000000E-01

    7.500000E-01

    8.750000E-01

    9.375000E-01

    9.687500E-01

    9.843750E-01

    9.921875E-01

    9.960938E-01

    9.980469E-01

    9.990234E-01

    9.995117E-01

    9.997559E-01

    9.998779E-01

    9.999390E-01

    9.999695E-01

    9.999847E-01

    9.999924E-01

    9.999962E-01

    9.999981E-01

    9.999990E-01

    9.999995E-01

    9.999998E-01

    9.999999E-01

    9.999999E-01

        1.000000

        1.000000

        1.000000

        1.000000

        1.000000

        1.000000

 

 

Problem#10

An object falls 5 meters during the first second, 15 meters during the second, 25 meters during the third, 35 meters during the fourth, and so on. Thus the height fallen is given by the infinite series:

            5  +  15  +  25  +  35  +   45  +  .  .  .

Write a program to find the height fallen in 12 seconds. (DO NOT USE ANY READ STATEMENT IN YOUR PROGRAM).

Your output should be:

HEIGHT FALLEN IN      12  SECONDS IS      720 METERS

 

Problem#11

 

If we start with two different positive integers a and b and repeatedly replace the larger value by their absolute difference, stopping when both numbers are the same, this final value is the greatest common divisor (GCD) of the two original numbers. Write a program that reads two positive integers and then it prints their GCD and their LCM (Least Common Multiple) given that:

            GCD(a,b) * LCM(a,b) = a * b

 

Test your program with the data:

540      504

 

Your output should be:

 

THE GCD OF     540 AND     504 IS         36

THE LCM OF     540 AND     504 IS     7560

 

Problem#12

 

Write a program that calculates the smallest power of 2 exceeding a bound input to the program. For example for an input of 21 the program should output:

            SMALLEST POWER OF 2 GREATER THAN     21 IS    5

            2 RAISED TO THE POWER OF      5 IS     32

 

Problem#13

 

Write a program that reads a positive integer N and then it prints the smallest positive integer K such that: 3 K  ³  N

 

Use the data: 236

Your output should be:

5

 

 

Problem#14

 

Write a program that reads a set of ball-bearing diameter-readings terminated by the sentinel -1.0. A ball-bearing is classified as faulty if its diameter is less than 1.99 mm or greater than 2.01 mm. Your program should report the percentage of faulty ball-bearings in the sample.

 

Use the following data:

 

1.99

2.00

2.01

2.00

1.98

2.02

2.01

1.97

2.03

2.00

-1.00

 

Your output should be:

 

THE PERCENTAGE OF FAULTY BALL-BEARINGS =       40.000000

 

Problem#15

 

Write a program that will read a positive REAL number X and will then read positive REAL numbers until -1.0 is read. The program should print a message indicating which of these numbers is closest to X.

 

Use the data:

5.2

3.0

2.0

6.0

4.5

7.0

4.4

-1.0

 

Your output should be:

THE  CLOSEST NUMBER TO     5.200000 IS     4.500000

 

 

Problem#16

 

Write a program that calculates the highest average temperature for several cities where the temperature was read on 3 days. Use the following data:

            1          35        30        40

            2          10        15        16

            3          36        38        39

            4          38        35        30

            -1         -1         -1         -1

where in the first column are the city numbers and in the rest of the columns are the temperature readings. The last data line is the sentinel.

 

Your output should be:

 

MAXIMUM AVERAGE TEMPERATURE =     37.666670

 

 

Problem#17

 

The distance s and the height h of an object after t seconds from being projected at an initial velocity v and an angle of projection q are given by:

            s  =  v t cos q

            h  =  v t sin q - 0.5 g t 2

where g = 9.8 m / s 2  is the acceleration due to gravity. Given that v  =  35.0 m / s and        q  =  0.78539 Radians (i.e. 45 0 ). Write a program to tabulate the values for height and distance for t = 0.1, 0.4, 0.7, 1.0, . . .   as long as the height h is positive.

 

Your output should be:

            TIME (S)            HEIGHT (M)   DISTANCE (M)

            1.000000E-01        2.425854        2.474894

            4.000000E-01        9.115415        9.899575

            7.000000E-01       14.922980       17.324260

                    1.000000       19.848540       24.748940

                    1.300000       23.892100       32.173620

                    1.600000       27.053660       39.598300

                    1.900000       29.333220       47.022980

                    2.200000       30.730780       54.447660

                    2.500000       31.246340       61.872340

                    2.800000       30.879900       69.297020

                    3.100000       29.631460       76.721700

                    3.400000       27.501020       84.146390

                    3.700000       24.488590       91.571060

                    4.000000       20.594150       98.995740

                    4.300000       15.817710      106.420400

                    4.600000       10.159260      113.845100

                    4.900000         3.618821      121.269800

 

 

Problem#18

 

Write a FORTRAN program that reads the marks of students in an exam.  Assume the followings:

1.   the number of the students in the class is not known before hand. 

2.   each mark is put in a separate line

3.   a negative number is put after the last mark.

After reading all marks, your program should print the number of students in the class and the average they got in that exam.

 

Problem#19

 

Write a program that calculates the cost of each call and the total phone bill of a given customer.  Each line of the input to the program consists of two numbers:

duration of the call in minutes:  a real number

cost of the call per minute:  a real number (Riyals)

The last input line is the sentinel 0.0  0.0.

Use the following data:

 

2.5                   0.56

5.8                   16.00

3.0                     4.50

6.3                     8.00

0.0                     0.0

 

Your output should be:

 

DURATION                                       RATE                              COST

2.500000                                 5.600000E-01                          1.400000

5.800000                                        16.000000                        92.800000

3.000000                                          4.500000                        13.500000

6.300000                                          8.000000                        50.400000

 

TOTAL                                                                                  158.100000

 

Problem#20

 

Write a program that finds an approximation for p using the following infinite series:

 

p2 / 6 =  1 / 12  +  1 / 22  + 1 / 32  +  .  .  .

 

Your program should stop if the difference between two successive approximations is less than 0.000001.

 

 

Problem#21

 

Write a program that finds an approximation for p using the following infinite series:

 

p / 4 =  1   -   1 / 3   +   1 / 5   -   1 / 7   +   1 / 9    -  .  .  .

 

Your program should stop if the difference between two successive approximations is less than 0.000001.

 

Problem#22

 

Write a FORTRAN program that calculates the phone bill of a given number of customers.  The first line of the input indicates the number of customers.  Each of the other lines of input consists of two numbers:

duration of the call in minutes:  a real number

cost of the call/ minute:  a real number (Saudi Riyals)

 

The last input line of each customer consists of two zeros.

The program calculates the total amount of each customer’s bill  in Saudi Riyals.  Print the output in the following manner:

THE TOTAL CHARGE OF CUSTOMER # 1 IS   *******   RIYALS

THE TOTAL CHARGE OF CUSTOMER # 2 IS   *******   RIYALS

                           . . . . . .

Hint: Use a while-loop within a do-loop

 

Test your program with the data:

 

3          ß-----------------  Number of Customers

1.5                   0.50

3.0                   17.00

0.0                   0.0       ß-----  End of Customer#1 data

2.3                   4.00

0.0                   0.0       ß----- End of Customer#2 data

5.0                   3.50

2.0                   8.00

6.0                   12.00

0.0                   0.0       ß---- End of Customer#3 data

 

Your output should be:

 

TOTAL CHARGE FOR CUSTOMER#          1    IS               51.750000  RIYALS

TOTAL CHARGE FOR CUSTOMER#          2    IS                 9.200000  RIYALS

TOTAL CHARGE FOR CUSTOMER#          3    IS             105.500000  RIYALS

 

 

Problem#23

 

Write a FORTRAN program that reads prices of new cars and determine if it is cheap, average, or expensive.  The category of the price of a car is determined according to the following:

cheap:  if the price of the car is less than 50,000 Riyals

expensive:  if the price of the car is greater than or equal to 50,000 Riyals and less than or equal to 100,000 Riyals

very expensive:  if the price of the car is greater than 100,000 Riyals

The program shall print an appropriate message such as:

CAR # **    IS    ***********

 

where *********** would be either CHEAP, EXPENSIVE, or VERY EXPENSIVE

 

Your program should iterate until it receives a zero input

 

Test your program with the following data:

 

80000.00

120000.00

30000.00

75000.00

0.0

 

Your output should be:

 

CAR#         1  IS EXPENSIVE

CAR#         2  IS VERY EXPENSIVE

CAR#         3  IS CHEAP

CAR#         4  IS EXPENSIVE

 

Problem#24

 

Let N be a positive integer consisting of up to 10 digits: d10d9d8 . . . d1 . Write a program to list in one column each of the digits in the number N. The rightmost digit d1 should be at the top of the column.

 

 

Problem#25(*)           

                                                                         __      

The Nth root of a positive number X, i.e., N Ö X   ,  for any positive integer N can be approximated as accurately as required by the approximations Y0 , Y1 , Y2 , Y3 , . . . where:

            Y0  =  X

            Y1  =   (1.0 / N ) * ( ( N - 1 ) * Y0  +  X / Y0 ( N - 1 )  )

            Y2  =   (1.0 / N ) * ( ( N - 1 ) * Y1  +  X / Y1 ( N - 1 )  )

            Y3  =   (1.0 / N ) * ( ( N - 1 ) * Y2  +  X / Y2 ( N - 1 )  )

              .

              .

              .

            Yi   =   (1.0 / N ) * ( ( N - 1 ) * Yi - 1  +  X / Yi - 1 ( N - 1 )  )

              .

              .

              .

Write a program which will prompt for and read the value of a positive number X and the value for N and compute the Nth root of X. Your program should stop approximating the required root if:

            | Yi  -  Yi - 1 |  £  0.0000001

 

Problem#26(*)

 

Write a main program which reads a real number X it then prints the real number and its cube root. The cube root must be computed by a function CBROOT invoked by the main program. The program output should be in the form:

 

NUMBER =    XXX.XXXXXXX  CUBE ROOT =  X.XXXXXXX

                       

The function CBROOT  computes the cube root of a number C  by successively approximating the cube root as X0 , X1 , X2 , X3 ,  . . . where:

 

            X0  =  C

            X1  =  (1 / 3.0) * (2X0  +  C / X02 )

            X2  =  (1 / 3.0) * (2X1  +  C / X12 )

            X3  =  (1 / 3.0) * (2X2  +  C / X22 )

                                    .

                                    .

                                    .

 

The function returns the approximated value of each cube root to the main program when the absolute difference between two successive approximations Xn and Xn + 1 is less than 0.00001. i.e.: | Xn + 1 - Xn |  <    0.00001

 

Use the following data:

729.0

 

 

Problem#27(*)

Write a program which will read the initial balance of a bank customer and output a bank statement for that customer. The minimum account balance is 100 Riyals.

 

The first data line contains the initial balance of the customer. Each of the remaining data lines, except the last, contains two integers: a code (1 for Deposit, 2 for Withdrawal) and the amount to be deposited or withdrawn. The last data line contains the sentinel: 0       0 . ASSUME THAT NO INVALID DATA IS READ.

 

Your program must output:

     1. The initial balance.

2. The code, amount, and the new balance for each deposit or withdrawal. If a withdrawal is rejected (i.e. when the minimum balance becomes less than 100) the message REJECTED must also be printed.

 

 

Problem#28(***)

 

A football team gets 2 points for a win, 1 point for a draw, and no points for a loss. Write a program which will compute the goals for, goals against, and points for a particular team.

 

The first data line contains the integer code for the team. Each of the remaining data lines, except the last, contains four integers: a code for a first team, a code for a second team, goals scored by the first team, and goals scored by the second team. The last data line contains the sentinel 0  0  0 .

 

If the required team has played any match the program must output a table with the heading:

 

THE RESULTS OF TEAM  K :

 

TEAM AGAINST       GOALS FOR              GOALS AGAINST                 POINTS

 

The table should be filled with appropriate results and it should terminate with the total number of for and against team K and the total points for the team.

 

If team K has not played any match the above heading followed by the message: TEAM K HAS NOT PLAYED ANY MATCH is printed.

 

Use the following data:

 

4

2          4          3          2

1          3          1          0

4          1          4          2

3          4          2          2

2          3          2          5

1          2          0          0

0          0          0          0

 

Your output should be:

 

THE RESULTS OF TEAM  4 :

 

TEAM AGAINST       GOALS FOR              GOALS AGAINST                 POINTS

            2                                  2                                  3                                       0

            1                                  4                                  2                                       2

            3                                  2                                  2                                       1