Hey!
Let me ask you something first.
Can you count from 1 to 5? Of course you can.
Can you tell me all multiples of 3 between 1 and 30? Probably yes, but it takes time.
Can you find all divisors of 48? Now that takes a bit of thinking.
Python can do all three in seconds. With a while loop.
Today we are going to solve 8 programs using only while loops. Each one builds on the last. By the end — you will be very comfortable with while loops and number logic.
Let us start.
Before We Begin — How a While Loop Works
i = 1
while i <= 5:
print(i)
i += 1
Output:
1
2
3
4
5
i = 1 — starting point.
while i <= 5 — keep running as long as i is 5 or less.
i += 1 — increase i by 1 every step. Without this — infinite loop.
Every program below follows this same structure. Start somewhere. Run until a condition. Move forward each step.
1. Print 1 1 1 1 1
Print the number 1 five times.
Quick question for you.
What do you think changes in the loop if you want to print the same number every time instead of increasing?
Nothing changes in the loop — only the print statement. You just print a fixed value instead of i.
i = 1
while i <= 5:
print(1, end=" ")
i += 1
Output:
1 1 1 1 1
end=" " — prints everything on the same line with a space between. Without it — each number prints on a new line.
i is still counting the steps. But we print 1 every time instead of i.
2. Print 1 2 3 4 5
Print numbers from 1 to 5 in order.
i = 1
while i <= 5:
print(i, end=" ")
i += 1
Output:
1 2 3 4 5
This time we print i directly. i starts at 1 and goes up by 1 each step.
Simple. But this is the base pattern for everything else.
3. Print 1 3 5 7 9 — Odd Numbers
Print the first five odd numbers.
Quick question for you.
What is the pattern here? How much does each number increase by?
Each number increases by 2. 1 → 3 → 5 → 7 → 9.
So instead of i += 1 — we use i += 2.
i = 1
while i <= 9:
print(i, end=" ")
i += 2
Output:
1 3 5 7 9
Start at 1. Jump by 2 each time. Stop at 9.
The jump size is what creates the pattern.
4. Print 3 6 9 12 15 — Multiples of 3
Print the first five multiples of 3.
Same idea — just start at 3 and jump by 3 each time.
i = 3
while i <= 15:
print(i, end=" ")
i += 3
Output:
3 6 9 12 15
Start at 3. Add 3 every step. Stop at 15.
4.1 Print 15 12 9 6 3 — Reverse
Now print the same series backwards.
Quick question for you.
If going forward means i += 3 — what do you think going backward means?
i -= 3. We start at the biggest number and subtract each time.
i = 15
while i >= 3:
print(i, end=" ")
i -= 3
Output:
15 12 9 6 3
Start at 15. Subtract 3 each step. Stop when i goes below 3.
4.2 Print 10 8 6 4 2 — Reverse Even Numbers
Start at 10. Go down. Jump by 2.
i = 10
while i >= 2:
print(i, end=" ")
i -= 2
Output:
10 8 6 4 2
Start at 10. Subtract 2 each step. Stop when i goes below 2.
4.3 Print 9 7 5 3 1 — Reverse Odd Numbers
Start at 9. Go down. Jump by 2.
i = 9
while i >= 1:
print(i, end=" ")
i -= 2
Output:
9 7 5 3 1
Start at 9. Subtract 2 each step. Stop when i goes below 1.
Quick Recap — The Pattern So Far
Before we move to harder programs — let us notice what we learned.
Going forward — start small, use +=, condition checks upper limit.
Going backward — start big, use -=, condition checks lower limit.
Jump size — controls the gap between numbers. += 1 gives every number. += 2 gives every other number. += 3 gives multiples of 3.
That is all that changes between the programs above. Same loop structure. Different start, step, and direction.
5. Multiples of 3 AND 5
Print numbers from 1 to 50 that are divisible by both 3 and 5.
A number divisible by both 3 and 5 is divisible by 15. Like 15, 30, 45.
Quick question for you.
How do you check if a number is divisible by 3 and 5 at the same time?
Use % — the modulo operator. It gives the remainder.
15 % 3 = 0 — 15 is divisible by 3.
15 % 5 = 0 — 15 is divisible by 5.
If both remainders are 0 — divisible by both.
i = 1
while i <= 50:
if i % 3 == 0 and i % 5 == 0:
print(i, end=" ")
i += 1
Output:
15 30 45
The loop checks every number from 1 to 50. Only when both conditions are true — it prints.
6. Multiples of 3 OR 5
Print numbers from 1 to 20 that are divisible by 3 or 5 — or both.
The only change — and becomes or.
i = 1
while i <= 20:
if i % 3 == 0 or i % 5 == 0:
print(i, end=" ")
i += 1
Output:
3 5 6 9 10 12 15 18 20
Quick question for you.
Why does 15 appear in this list too?
Because 15 is divisible by both 3 and 5. or means — if at least one condition is true. 15 satisfies both, so it definitely satisfies or.
Notice the difference from program 5. Program 5 gave only 15, 30, 45. Program 6 gives many more numbers because the condition is less strict.
7. Divisors of a Given Number
A divisor is a number that divides another number exactly — with no remainder.
Divisors of 12 are: 1, 2, 3, 4, 6, 12.
How do we find them? Check every number from 1 to the given number. If it divides evenly — it is a divisor.
n = int(input("Enter a number: "))
i = 1
while i <= n:
if n % i == 0:
print(i, end=" ")
i += 1
Input: 12
Output:
1 2 3 4 6 12
n % i == 0 — if dividing n by i gives remainder 0, then i is a divisor of n.
The loop checks every number from 1 to n. Any that divides evenly gets printed.
Quick question for you.
What are the divisors of 7?
Try it mentally. Which numbers divide 7 exactly?
Only 1 and 7. Because 7 is a prime number. It has only two divisors — 1 and itself.
Run the program with input 7 and confirm.
8. Count of Divisors of a Given Number
Same as program 7 — but instead of printing divisors, we just count them.
n = int(input("Enter a number: "))
i = 1
count = 0
while i <= n:
if n % i == 0:
count += 1
i += 1
print("Number of divisors:", count)
Input: 12
Output:
Number of divisors: 6
count = 0 — start with 0.
Every time we find a divisor — count += 1.
At the end — print the total count.
Input: 7
Output:
Number of divisors: 2
7 has 2 divisors — 1 and 7.
Quick question for you.
Which number between 1 and 20 do you think has the most divisors?
Try to guess. Then run the program for each number from 1 to 20 and check. The answer might surprise you.
It is 12. It has 6 divisors — 1, 2, 3, 4, 6, 12.
Quick Summary — What These 8 Programs Teach
-
Same number — print a fixed value, not
i. -
Sequence — print
idirectly as it increases. - Odd numbers — start at 1, jump by 2.
- Multiples — start at the number, jump by that number.
- Reverse — start big, subtract instead of add.
- AND condition — both must be true. Stricter. Fewer results.
- OR condition — at least one must be true. Looser. More results.
-
Divisors — check every number from 1 to n using
%. Count or print matches.
All 8 programs use the same simple while loop structure. What changes is the starting point, the jump size, the direction, and the condition inside.
Once you understand those four things — you can build any number pattern.
Try modifying the programs. What if you want multiples of 4 from 1 to 40? What if you want divisors of 100? What if you want to print in reverse from 20 to 1 jumping by 3?
Make the changes. Run the code. See it work.
That is when while loops really click.
If you have a question — drop it in the comments below.
Thanks for reading. Keep building.
Top comments (0)