Loops let you run the same code multiple times. Instead of writing print() ten times, you write it once inside a loop.
What you'll learn
forloops to iterate a known number of timesrange()to generate sequences of numberswhileloops for condition-based repetitionbreakandcontinuefor loop control
Instructions
- Write a
forloop that prints the multiplication table for a number read from stdin. - Print each line as:
"N x i = result"for i from 1 to 10.
Bonus challenge
- After the table, use a
whileloop to find the smallest power of 2 that is greater than the input number. Print it. (This part is optional — try it once your for loop works!)
Example output for input 7:
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70
Smallest power of 2 greater than 7: 8
Key concepts
for loop with range():
for i in range(1, 11): # i = 1, 2, 3, ..., 10
print(i)
range(start, stop) generates numbers from start up to (but not including) stop.
while loop runs as long as its condition is true:
power = 1
while power <= n:
power = power * 2
break exits the loop immediately. continue skips to the next iteration.
Hints
range(1, 11)gives you 1 through 10 (the stop value is exclusive).- For the power-of-2 part, start at 1 and keep doubling until you exceed the input.
- Make sure your while loop has a condition that eventually becomes false — otherwise it runs forever.