Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Lab 4

1 The while loop


2 The do-while loop
3 The for loop
4 Continue
5 Break
6 H.W
The while loop

Write a program that prompts the user to enter an answer for a question
on addition of two single digits. Using a loop, you can now rewrite the
program to let the user repeatedly enter a new answer until it is correct,
as shown in.

2
Write a program that reads and calculates the sum of an unspecified number of
integers. The input 0 signifies the end of the input. Do you need to declare a new
variable for each input value? No. Just use one variable named data to store the
input value and use a variable named sum to store the total. Whenever a value is
read, assign it to data and, if it is not zero, add it to sum

3
The do-while loop

The difference between a while loop and a do-while loop is the order in which
the loop-continuation-condition is evaluated and the loop body executed. You can write
a loop using either the while loop or the do-while loop. Sometimes one is a more
convenient choice than the other. For example, you can rewrite the while loop using
a do-while loop

4
The for loop

5
Multiplication Table

6
Continue

7
Break

8
H.W
1. (Count positive and negative numbers and compute the average of numbers)
Write a program that reads an unspecified number of integers, determines
how many positive and negative values have been read, and computes the
total and average of the input values (not counting zeros). Your program ends
with the input 0. Display the average as a floating-point number. Here is a
sample run

2. (Financial application: compute future tuition) Suppose that the tuition for a
university is $10,000 this year and increases 5% every year. In one year, the
tuition will be $10,500. Write a program that computes the tuition in ten years
and the total cost of four years’ worth of tuition after the tenth year.

3. (Find the two highest scores) Write a program that prompts the user to enter
the number of students and each student’s name and score, and finally
displays the student with the highest score and the student with the second-
highest score

9
4. (Display four patterns using loops) Use nested loops that display the following
patterns in four separate programs:

5. Write a loop that computes


a) The sum of all even numbers between 2 and 100 (inclusive).
b) The sum of all squares between 1 and 100 (inclusive).
c) The sum of all odd numbers between a and b (inclusive).
d) The sum of all odd digits of n. (For example, if n is 32677, the sum would be
3 + 7 + 7 = 17.)

6. Write a program that reads a set of floating-point values. Ask the user to enter
the values (prompting only a single time for the values), then print
• the average of the values.
• the smallest of the values.

• the largest of the values.


• the range, that is the difference between the smallest and largest.

10
7. Rewrite the following do loop into a while loop.

8. Rewrite the following for loop into a while loop

11

You might also like