Loops Practice Assignments

Overview

Loops are foundational for computer programming. Almost every program uses at least one loop.

Note

Do not use function calls instead of looping. Make sure the your programs have a while loop or a for loop.

Submission Instructions

Submit each of the following practice assignments at codePost.io. To register for a free codePost account, please follow these instructions.
Watch this short video for a demonstration of submitting an assignment and reviewing the results:
YouTube video: codePost: Submission and Checking Results Thumbnail
Note, currently, feedback is not visible in Safari.

Practice Assignments

Numeric Series - while Loops

Write a Java program that requests the following three int values from the user:

  1. a starting number
  2. a ending number
  3. a step (increment) size
Use a while loop to display all of the numbers, from the starting number until (but not including) the ending number, incrementing by the step amount.
Note, you can not have a for loop in your program.

Save your Java program in a file named NumericSeriesWhileLoops.java.

Example 1:

Please enter the starting number: 10
Please enter the ending number: 22
Please enter the step size: 3
The numbers in the series that starts with 10, goes to (but not including) 22 by steps of 3 are:
10
13
16
19
(Notice that 22 is not included in the output)

Example 2:

Please enter the starting number: 10
Please enter the ending number: 0
Please enter the step size: -1
The numbers in the series that starts with 10, goes to (but not including) 0 by steps of -1 are:
10
9
8
7
6
5
4
3
2
1
(Notice that the increment size is negative)

Example 3:

Please enter the starting number: 1000
Please enter the ending number: 10
Please enter the step size: 51
The numbers in the series that starts with 1000, goes to (but not including) 10 by steps of 51 are:
Empty set
(Notice that Empty set is required)

Hints:

  1. It may be helpful for you to handle the case separately where the third value is positive and the case when it is negative.

Numeric Series - for Loops

Write a Java program that requests the following three int values from the user:

  1. a starting number
  2. a ending number
  3. a step (increment) size
Use a for loop to display all of the numbers, from the starting number until (but not including) the ending number, incrementing by the step amount.
Note, you can not have a while loop in your program.

Save your Java program in a file named NumericSeriesForLoops.java.

Example 1:

Please enter the starting number: 10
Please enter the ending number: 22
Please enter the step size: 3
The numbers in the series that starts with 10, goes to (but not including) 22 by steps of 3 are:
10
13
16
19
(Notice that 22 is not included in the output)

Example 2:

Please enter the starting number: 10
Please enter the ending number: 0
Please enter the step size: -1
The numbers in the series that starts with 10, goes to (but not including) 0 by steps of -1 are:
10
9
8
7
6
5
4
3
2
1
(Notice that the increment size is negative)

Example 3:

Please enter the starting number: 1000
Please enter the ending number: 10
Please enter the step size: 51
The numbers in the series that starts with 1000, goes to (but not including) 10 by steps of 51 are:
Empty set
(Notice that Empty set is required)

Hints:

  1. It may be helpful for you to handle the case separately where the third value is positive and the case when it is negative.