Java

REVIEW TOPIC 03 – LOOPS

 

LESSON WORK

 

 

QUESTION 1


a) Use a for loop to output numbers 1 to 10 on the screen. 

 

b) Use a while loop to output numbers 1 to 10 on the screen.

 

c) Alter your programs so that the numbers 1 to 10 all appear on the same line separated by a comma.

 

Save your work inside a file called Q1a.java and Q1b.java inside the folder called Topic03.  You can also put both programs in the same file if you prefer.

 

QUESTION 2

 

Create a looping program that outputs the numbers 1, 7, 13, 19, … for all numbers less than 100.  Use a for loop.

 

Save your work inside a file called Q2.java inside the folder called Topic03.

 

QUESTION 3


Create a looping program that outputs the following sequence to screen one number a time:

 

1, 2, 4, 7, 11, 16, 22, 29, …

 

You should continue until the number to output exceeds 200.

 

Save your work inside a file called Q3.java inside the folder called Topic 03.


QUESTION 4

 

Create a program that will output all numbers from 1 and 100 on a separate line.  After numbers 5, 10, 15, …, there should be a blank line left.

 

 

Partial sample output:

1

2

3

4

5

 

6

7

8

9

10

 

11

12

.

.

.

 

Save your work inside a file called Q4.java inside the folder called Topic 03.


QUESTION 5

 

Create a program that gets n numbers from the user and outputs the maximum value.  The value of n is inputted as the first number.

 

Sample input:

 

         4

         3

         6

         2

         5

 

Resulting output:

 

                   The max is 6.

 

Explanation:

 

On the first line of the input, the user specifies there are 4 numbers to read.  Of those four numbers, the maximum value is 6.

 

Save your work inside a file called Q5.java inside the folder called Topic 03.

 

QUESTION 6 (TIME PERMITTING)

 

Write a program that will output all Mersenne numbers below 1000.  Note that a Mersenne number is simply a number that is one less than a square number.  So 3, 8, 15 and 24 are the first few Mersenne numbers.