1.Introduction
What do multiples of numbers, divisors, and prime numbers have in common?
At first glance, they may seem like simple math concepts we learned in school. But behind the scenes, these are some of the most common problems programmers solve using loops and conditions.
Imagine asking a computer questions like:
Which numbers between 1 and 100 are divisible by both 3 and 5?
How can I find all divisors of a number?
How many divisors does a number have?
Is a given number prime or not?
The answer to all these questions starts with one fundamental programming concept: the while loop.
In this article, we'll use a series of simple Python programs to explore how while loops work and how they can be used to solve real mathematical problems. Whether you're a beginner learning Python or someone looking to strengthen your problem-solving skills, these examples will help you understand the power of loops in a practical way.
Let's dive in and see how a few lines of Python code can solve some interesting number-based challenges.
2. Finding Numbers Divisible by Both 3 and 5
Let's start with a simple problem.
We need to print all numbers between 1 and 100 that are divisible by both 3 and 5.
no = 1
while no <= 100:
if no % 3 == 0 and no % 5 == 0:
print(no)
no += 1
else:
print("false")
How it Works
The loop starts from 1 and continues until 100.
% is the modulus operator that returns the remainder.
If a number leaves a remainder of 0 when divided by both 3 and 5, it gets printed.
Such numbers are multiples of 15.
Output
15
30
45
60
75
90
3.Finding Numbers Divisible by Either 3 or 5
Now let's print numbers that are divisible by 3 or 5.
no = 1
while no <= 100:
if no % 3 == 0 or no % 5 == 0:
print(no)
no += 1
else:
print("false")
How it Works
Here we use the or operator instead of and.
If a number is divisible by either 3 or 5, it will be printed.
Examples
3
5
6
9
10
12
15
...
4. Finding the Divisors of a Number
A divisor is a number that divides another number completely without leaving a remainder.
Let's find all divisors of 20.
no = 20
i = 1
while i <= no:
if no % i == 0:
print(i)
i += 1
How it Works
Start checking from 1.
If 20 is divisible by the current value of i, print it.
Continue until i reaches 20.
Output
1
2
4
5
10
20
5. Counting the Number of Divisors
Instead of just printing divisors, we can count them as well.
no = 12
i = 1
count = 0
while i <= no:
if no % i == 0:
print(i)
count += 1
i += 1
print("Count =", count)
How it Works
Every time a divisor is found, increase count by 1.
After the loop finishes, print the total count.
Output
1
2
3
4
5
6
12
Count = 6
Since 12 has six divisors, the count is 6.
6.Checking Whether a Number is Prime
A prime number has exactly two divisors:
1
The Number Itself
Let's check whether 13 is prime.
no = 13
i = 1
count = 0
while i <= no:
if no % i == 0:
count += 1
i += 1
if count == 2:
print("Prime Number")
else:
print("Not a Prime Number")
How it Works
Count the total number of divisors.
If the count is exactly 2, the number is prime.
Otherwise, it is not prime.
Output
Prime Number
Because 13 is divisible only by 1 and 13.
7.Why These Programs Matter
These examples may look simple, but they introduce several important programming concepts:
While loops
Conditional statements
Modulus operator (%)
Counting Logic
Number theory fundamentals
Understanding these concepts will help you solve more advanced coding problems in the future.
8.Final Takeaway
The while loop is one of the most powerful tools for beginners learning Python. By combining loops with conditions, you can solve a variety of mathematical problems such as finding multiples, identifying divisors, counting factors, and checking prime numbers.
Master these basics, and you'll build a strong foundation for tackling more complex programming challenges.
Top comments (0)