169,245 views
42 votes
42 votes
(Python coding)4.3 Code Practice: Question 2

instructions:
Write a program that uses a while loop to calculate and print the multiples of 3 from 3 to 21. Your program should print each number on a separate line.

Expected Output:
3
6
9
12
15
18
21

User The Bndr
by
2.2k points

1 Answer

23 votes
23 votes

Answer:

i = 3

while i <= 21:

print(i)

i+=3

Step-by-step explanation:

First, we will declare i as 3 because otherwise Python will yell at you and say there is no variable with i, we set it to 3 because we will print then increment in the while loop instead of increment then print

Next the while loop, it will go until i is less than or equal to 21 to print from 3 to 21

Then we will print i as an output. The print command will print each number on a separate line

After that we will increment i by 3 to find the next multiple of 3

This solution will print the following:

3

6

9

12

15

18

21

User Vyrotek
by
3.1k points