Respuesta :
Answer:
// 1. Declare a main class to run the application
public class MultiplesOf3 {
// 2. Write the main method
public static void main(String[] args) {
// 3. For indexing, declare and initialize (3)
// an integer variable i with three. Since the number starts at three.
int i = 3;
// 4. Since the number ends at 21,
// Write the while statement with a condition of i being less than or
// equal to 21.
while (i <= 21){
// as long as i <= 21, print the value of i.
System.out.println(i);
// increment the value of i by 3.
i += 3;
} // end of while loop
} // end of main method
} // end of main class
Explanation:
The code above has been written in Java.
The code contains comments to explain each line of the code. Please go through the comments carefully for more understandability and readability of the code.
The source code (with no comments) has been attached to this reponse. You may download it for a better formatting of the code and to run it on your local machine.
Nevertheless, a few things are worth noting:
i. It is important to declare an indexing variable that will serve as a counter and increment at every cycle in the loop. In this case we have declared i = 3. We have chosen 3 because the sequence of numbers (multiples of 3 from 3 to 21) starts with 3.
ii. It is also important to note that the while loop runs only on one condition - when i is less than or equal to 21 (i <= 21). Once the condition is false, the loop terminates and no longer execute. We have chosen that condition (i < = 21) because the sequence of numbers (multiples of 3 from 3 to 21) ends with 21.
iii. The while loop statement also has a line where i is being incremented by 3. i.e (i+=3). This is important since we are printing the multiples of 3. So the sequence (3 6 9 12 15 18 21) increases by 3.
iv. In Java, to print each of the numbers on a separate line, we use the:
System.out.println() method.
v. When the loop first starts, i will be 3 and since 3 is less than 21, 3 will be printed to the console and i will be incremented by 3 to become 6. In the second cycle of the loop, i will be 6 and since 6 is less than 21, 6 will be printed to the console and i will be incremented by 3 to become 9. This process will continue until i is greater than 21. In which case the condition is no longer satisfied and so the loop terminates.
The ouptut of the program is as follows:
3
6
9
12
15
18
21
Hope this helps!