DEV Community

Cover image for Python While Loop Programs — Divisors, Primes, Factorial and More
DHANRAJ S
DHANRAJ S

Posted on

Python While Loop Programs — Divisors, Primes, Factorial and More

Hey!

Welcome back.

In the last blog — we covered patterns, multiples, and divisors using while loops.

Today we go deeper.

Prime numbers. Perfect numbers. Factorial. Odd and even in one loop. Sum programs. All using only while loops.

Each program builds your thinking. By the end — you will be very comfortable writing loops with conditions.

Let us start.


1. Prime Number

A prime number is a number that has exactly 2 divisors — 1 and itself.

2, 3, 5, 7, 11, 13 are prime numbers.
4, 6, 8, 9, 10 are not — they have more than 2 divisors.

Quick question for you.

Is 1 a prime number?

No. 1 has only one divisor — itself. A prime number needs exactly 2 divisors. So 1 is not prime.

The logic — count divisors. If count is exactly 2 — it is prime.

n = int(input("Enter a number: "))

i = 1
count = 0

while i <= n:
    if n % i == 0:
        count += 1
    i += 1

if count == 2:
    print(n, "is a Prime Number")
else:
    print(n, "is NOT a Prime Number")
Enter fullscreen mode Exit fullscreen mode

Input: 7

Output:

7 is a Prime Number
Enter fullscreen mode Exit fullscreen mode

Input: 12

Output:

12 is NOT a Prime Number
Enter fullscreen mode Exit fullscreen mode

Input: 1

Output:

1 is NOT a Prime Number
Enter fullscreen mode Exit fullscreen mode

The logic is clean — count the divisors. Two divisors means prime. Anything else means not prime.


2. Perfect Number

A perfect number is a number where the sum of all its divisors (except itself) equals the number.

Example — 6. Divisors of 6 (excluding 6) are 1, 2, 3. Sum = 1 + 2 + 3 = 6. Equal to the number. So 6 is perfect.

Example — 28. Divisors of 28 (excluding 28) are 1, 2, 4, 7, 14. Sum = 1+2+4+7+14 = 28. Perfect.

Quick question for you.

Why do we exclude the number itself when checking for a perfect number?

Because if we include it — every number would have its divisor sum exceed itself. The definition of perfect number specifically means the sum of proper divisors — all divisors except the number itself.

n = int(input("Enter a number: "))

i = 1
total = 0

while i <= n//2:       
    if n % i == 0:
        total += i
    i += 1

if total == n:
    print(n, "is a Perfect Number")
else:
    print(n, "is NOT a Perfect Number")
Enter fullscreen mode Exit fullscreen mode

Input: 6

Output:

6 is a Perfect Number
Enter fullscreen mode Exit fullscreen mode

Input: 28

Output:

28 is a Perfect Number
Enter fullscreen mode Exit fullscreen mode

Input: 10

Output:

10 is NOT a Perfect Number
Enter fullscreen mode Exit fullscreen mode

Notice while i <= n//2 — not i <= n. We stop before reaching the number itself so we never include it in the sum.


3. Print 1 3 5 7 9 2 4 6 8 10 — Odd Then Even in a Single Loop

Print all odd numbers from 1 to 9, then all even numbers from 2 to 10 — in a single loop.

Quick question for you.

Think about this before reading the code. How would you print odd and even numbers in one loop?

The trick — run the loop 10 times. In the first 5 steps, print odd. In the next 5 steps, print even.

i = 1

while i <= 10:
    if i <= 5:
        print(2 * i - 1, end=" ")   # odd numbers: 1 3 5 7 9
    else:
        print(2 * (i - 5), end=" ") # even numbers: 2 4 6 8 10
    i += 1
Enter fullscreen mode Exit fullscreen mode

Output:

1 3 5 7 9 2 4 6 8 10
Enter fullscreen mode Exit fullscreen mode

Let us trace through the logic.

When i = 12*1 - 1 = 1
When i = 22*2 - 1 = 3
When i = 32*3 - 1 = 5
When i = 42*4 - 1 = 7
When i = 52*5 - 1 = 9
When i = 62*(6-5) = 2
When i = 72*(7-5) = 4
When i = 82*(8-5) = 6
When i = 92*(9-5) = 8
When i = 102*(10-5) = 10

One loop. Both odd and even. Clean.


4. Factorial of a Given Number

Factorial of a number means multiply it with every number below it down to 1.

Factorial of 5 = 5 × 4 × 3 × 2 × 1 = 120

Written as 5!

Factorial of 0 = 1 (by definition)
Factorial of 1 = 1

Quick question for you.

What is 4!?

4 × 3 × 2 × 1 = 24.

n = int(input("Enter a number: "))

i = 1
result = 1

while i <= n:
    result = result * i
    i += 1

print("Factorial of", n, "is:", result)
Enter fullscreen mode Exit fullscreen mode

Input: 5

Output:

Factorial of 5 is: 120
Enter fullscreen mode Exit fullscreen mode

Input: 0

Output:

Factorial of 0 is: 1
Enter fullscreen mode Exit fullscreen mode

When n = 0 — the while loop condition i <= 0 is false immediately. Loop never runs. result stays 1. Which is the correct answer.

Let us trace 5!:

result = 1
i=1 → result = 1 * 1 = 1
i=2 → result = 1 * 2 = 2
i=3 → result = 2 * 3 = 6
i=4 → result = 6 * 4 = 24
i=5 → result = 24 * 5 = 120
Enter fullscreen mode Exit fullscreen mode

5. Sum of First N Numbers

Add all numbers from 1 to n.

Sum of first 5 = 1 + 2 + 3 + 4 + 5 = 15.

n = int(input("Enter n: "))

i = 1
total = 0

while i <= n:
    total += i
    i += 1

print("Sum of first", n, "numbers:", total)
Enter fullscreen mode Exit fullscreen mode

Input: 5

Output:

Sum of first 5 numbers: 15
Enter fullscreen mode Exit fullscreen mode

Input: 10

Output:

Sum of first 10 numbers: 55
Enter fullscreen mode Exit fullscreen mode

total = 0 — starts at zero.
Each step adds i to total.
After the loop — total holds the sum.

Quick question for you.

There is actually a formula for this — n * (n + 1) / 2. For n = 10, that gives 10 * 11 / 2 = 55. Same answer.

So why use a loop?

Because loops are easier to understand and modify. What if you want the sum of only even numbers from 1 to n? The formula does not directly help. The loop does — just add if i % 2 == 0.


6. Print First 5 Prime Numbers

We already know how to check if a number is prime. Now we keep checking numbers one by one until we have found 5 primes.

count = 0
n = 2

while count < 5:
    i = 1
    divisors = 0

    while i <= n:
        if n % i == 0:
            divisors += 1
        i += 1

    if divisors == 2:
        print(n, end=" ")
        count += 1

    n += 1

print()
Enter fullscreen mode Exit fullscreen mode

Output:

2 3 5 7 11
Enter fullscreen mode Exit fullscreen mode

Let us understand the structure.

Outer loop — runs until we have found 5 primes. count < 5.

Inner loop — checks if current n is prime by counting divisors.

If n is prime — print it, increase count.

Move to next number — n += 1.

Two nested while loops. Outer controls how many primes we want. Inner checks if each number is prime.

Quick question for you.

Why do we start n at 2 and not 1?

Because 1 is not a prime number. It has only 1 divisor. Starting at 2 skips 1 naturally. 2 is the smallest prime number.


7. Sum of First 5 Prime Numbers

Same logic as program 14 — but instead of printing, we add each prime to a total.

count = 0
n = 2
total = 0

while count < 5:
    i = 1
    divisors = 0

    while i <= n:
        if n % i == 0:
            divisors += 1
        i += 1

    if divisors == 2:
        total += n
        count += 1

    n += 1

print("Sum of first 5 prime numbers:", total)
Enter fullscreen mode Exit fullscreen mode

Output:

Sum of first 5 prime numbers: 28
Enter fullscreen mode Exit fullscreen mode

First 5 primes are 2, 3, 5, 7, 11. Sum = 2+3+5+7+11 = 28.

The only change from program 14 — instead of print(n) we write total += n. And at the end we print the total.


Quick Summary — What Each Program Teaches

Program What It Teaches
Count of Divisors % operator, counting with a loop
Prime Number Divisor count == 2 means prime
Perfect Number Sum of proper divisors == number
Odd Then Even Math formula inside loop to generate patterns
Factorial Multiplying instead of adding in a loop
Sum of N Numbers Running total using total += i
First 5 Primes Nested loops — outer controls count, inner checks prime
Sum of First 5 Primes Same as above — add instead of print

The key idea connecting all these programs.

Every problem becomes simple when you break it into steps.

Is this number prime? → Count its divisors. If 2, yes.
Is this number perfect? → Sum its proper divisors. If equal, yes.
What is the factorial? → Multiply while counting down.

The loop just repeats those steps automatically.

Try modifying the programs.

What are the first 10 prime numbers?
What is the factorial of 10?
Sum of first 20 numbers?
Is 496 a perfect number?

Change the numbers. Run the code. Verify your answers.

That hands-on practice is where loops truly click.

If you have a question — drop it in the comments below.


Thanks for reading. Keep building.---
title: Python While Loop Programs — Divisors, Primes, Factorial and More
published: true

tags: python, beginners, programming, loops

Hey!

Welcome back.

In the last blog — we covered patterns, multiples, and divisors using while loops.

Today we go deeper.

Prime numbers. Perfect numbers. Factorial. Odd and even in one loop. Sum programs. All using only while loops.

Each program builds your thinking. By the end — you will be very comfortable writing loops with conditions.

Let us start.


Quick Reminder — How a While Loop Works

i = 1

while i <= 5:
    print(i)
    i += 1
Enter fullscreen mode Exit fullscreen mode

Start somewhere. Check the condition. Run the body. Move forward. Repeat until condition is false.

Every program today follows this same idea.


8. Count of 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. So count is 6.

Quick question for you.

How do you check if a number divides another exactly?

Using % — the modulo operator. It gives the remainder.

12 % 3 = 0 → 3 divides 12 exactly. Divisor.
12 % 5 = 2 → 5 does not divide 12 exactly. Not a divisor.

n = int(input("Enter a number: "))

i = 1
count = 0

while i <= n:
    if n % i == 0:
        count += 1
    i += 1

print("Count of divisors:", count)
Enter fullscreen mode Exit fullscreen mode

Input: 12

Output:

Count of divisors: 6
Enter fullscreen mode Exit fullscreen mode

Input: 7

Output:

Count of divisors: 2
Enter fullscreen mode Exit fullscreen mode

7 has only 2 divisors — 1 and 7. That means 7 is a prime number. We will use this idea in the next program.


9. Prime Number

A prime number is a number that has exactly 2 divisors — 1 and itself.

2, 3, 5, 7, 11, 13 are prime numbers.
4, 6, 8, 9, 10 are not — they have more than 2 divisors.

Quick question for you.

Is 1 a prime number?

No. 1 has only one divisor — itself. A prime number needs exactly 2 divisors. So 1 is not prime.

The logic — count divisors. If count is exactly 2 — it is prime.

n = int(input("Enter a number: "))

i = 1
count = 0

while i <= n:
    if n % i == 0:
        count += 1
    i += 1

if count == 2:
    print(n, "is a Prime Number")
else:
    print(n, "is NOT a Prime Number")
Enter fullscreen mode Exit fullscreen mode

Input: 7

Output:

7 is a Prime Number
Enter fullscreen mode Exit fullscreen mode

Input: 12

Output:

12 is NOT a Prime Number
Enter fullscreen mode Exit fullscreen mode

Input: 1

Output:

1 is NOT a Prime Number
Enter fullscreen mode Exit fullscreen mode

The logic is clean — count the divisors. Two divisors means prime. Anything else means not prime.


10. Perfect Number

A perfect number is a number where the sum of all its divisors (except itself) equals the number.

Example — 6. Divisors of 6 (excluding 6) are 1, 2, 3. Sum = 1 + 2 + 3 = 6. Equal to the number. So 6 is perfect.

Example — 28. Divisors of 28 (excluding 28) are 1, 2, 4, 7, 14. Sum = 1+2+4+7+14 = 28. Perfect.

Quick question for you.

Why do we exclude the number itself when checking for a perfect number?

Because if we include it — every number would have its divisor sum exceed itself. The definition of perfect number specifically means the sum of proper divisors — all divisors except the number itself.

n = int(input("Enter a number: "))

i = 1
total = 0

while i < n:       # notice: i < n, not i <= n
    if n % i == 0:
        total += i
    i += 1

if total == n:
    print(n, "is a Perfect Number")
else:
    print(n, "is NOT a Perfect Number")
Enter fullscreen mode Exit fullscreen mode

Input: 6

Output:

6 is a Perfect Number
Enter fullscreen mode Exit fullscreen mode

Input: 28

Output:

28 is a Perfect Number
Enter fullscreen mode Exit fullscreen mode

Input: 10

Output:

10 is NOT a Perfect Number
Enter fullscreen mode Exit fullscreen mode

Notice while i < n — not i <= n. We stop before reaching the number itself so we never include it in the sum.


11. Print 1 3 5 7 9 2 4 6 8 10 — Odd Then Even in a Single Loop

Print all odd numbers from 1 to 9, then all even numbers from 2 to 10 — in a single loop.

Quick question for you.

Think about this before reading the code. How would you print odd and even numbers in one loop?

The trick — run the loop 10 times. In the first 5 steps, print odd. In the next 5 steps, print even.

i = 1

while i <= 10:
    if i <= 5:
        print(2 * i - 1, end=" ")   # odd numbers: 1 3 5 7 9
    else:
        print(2 * (i - 5), end=" ") # even numbers: 2 4 6 8 10
    i += 1
Enter fullscreen mode Exit fullscreen mode

Output:

1 3 5 7 9 2 4 6 8 10
Enter fullscreen mode Exit fullscreen mode

Let us trace through the logic.

When i = 12*1 - 1 = 1
When i = 22*2 - 1 = 3
When i = 32*3 - 1 = 5
When i = 42*4 - 1 = 7
When i = 52*5 - 1 = 9
When i = 62*(6-5) = 2
When i = 72*(7-5) = 4
When i = 82*(8-5) = 6
When i = 92*(9-5) = 8
When i = 102*(10-5) = 10

One loop. Both odd and even. Clean.


12. Factorial of a Given Number

Factorial of a number means multiply it with every number below it down to 1.

Factorial of 5 = 5 × 4 × 3 × 2 × 1 = 120

Written as 5!

Factorial of 0 = 1 (by definition)
Factorial of 1 = 1

Quick question for you.

What is 4!?

4 × 3 × 2 × 1 = 24.

n = int(input("Enter a number: "))

i = 1
result = 1

while i <= n:
    result = result * i
    i += 1

print("Factorial of", n, "is:", result)
Enter fullscreen mode Exit fullscreen mode

Input: 5

Output:

Factorial of 5 is: 120
Enter fullscreen mode Exit fullscreen mode

Input: 0

Output:

Factorial of 0 is: 1
Enter fullscreen mode Exit fullscreen mode

When n = 0 — the while loop condition i <= 0 is false immediately. Loop never runs. result stays 1. Which is the correct answer.

Let us trace 5!:

result = 1
i=1 → result = 1 * 1 = 1
i=2 → result = 1 * 2 = 2
i=3 → result = 2 * 3 = 6
i=4 → result = 6 * 4 = 24
i=5 → result = 24 * 5 = 120
Enter fullscreen mode Exit fullscreen mode

13. Sum of First N Numbers

Add all numbers from 1 to n.

Sum of first 5 = 1 + 2 + 3 + 4 + 5 = 15.

n = int(input("Enter n: "))

i = 1
total = 0

while i <= n:
    total += i
    i += 1

print("Sum of first", n, "numbers:", total)
Enter fullscreen mode Exit fullscreen mode

Input: 5

Output:

Sum of first 5 numbers: 15
Enter fullscreen mode Exit fullscreen mode

Input: 10

Output:

Sum of first 10 numbers: 55
Enter fullscreen mode Exit fullscreen mode

total = 0 — starts at zero.
Each step adds i to total.
After the loop — total holds the sum.

Quick question for you.

There is actually a formula for this — n * (n + 1) / 2. For n = 10, that gives 10 * 11 / 2 = 55. Same answer.

So why use a loop?

Because loops are easier to understand and modify. What if you want the sum of only even numbers from 1 to n? The formula does not directly help. The loop does — just add if i % 2 == 0.


14. Print First 5 Prime Numbers

We already know how to check if a number is prime. Now we keep checking numbers one by one until we have found 5 primes.

count = 0
n = 2

while count < 5:
    i = 1
    divisors = 0

    while i <= n:
        if n % i == 0:
            divisors += 1
        i += 1

    if divisors == 2:
        print(n, end=" ")
        count += 1

    n += 1

print()
Enter fullscreen mode Exit fullscreen mode

Output:

2 3 5 7 11
Enter fullscreen mode Exit fullscreen mode

Let us understand the structure.

Outer loop — runs until we have found 5 primes. count < 5.

Inner loop — checks if current n is prime by counting divisors.

If n is prime — print it, increase count.

Move to next number — n += 1.

Two nested while loops. Outer controls how many primes we want. Inner checks if each number is prime.

Quick question for you.

Why do we start n at 2 and not 1?

Because 1 is not a prime number. It has only 1 divisor. Starting at 2 skips 1 naturally. 2 is the smallest prime number.


15. Sum of First 5 Prime Numbers

Same logic as program 14 — but instead of printing, we add each prime to a total.

count = 0
n = 2
total = 0

while count < 5:
    i = 1
    divisors = 0

    while i <= n:
        if n % i == 0:
            divisors += 1
        i += 1

    if divisors == 2:
        total += n
        count += 1

    n += 1

print("Sum of first 5 prime numbers:", total)
Enter fullscreen mode Exit fullscreen mode

Output:

Sum of first 5 prime numbers: 28
Enter fullscreen mode Exit fullscreen mode

First 5 primes are 2, 3, 5, 7, 11. Sum = 2+3+5+7+11 = 28.

The only change from program 14 — instead of print(n) we write total += n. And at the end we print the total.


Quick Summary — What Each Program Teaches

Program What It Teaches
Count of Divisors % operator, counting with a loop
Prime Number Divisor count == 2 means prime
Perfect Number Sum of proper divisors == number
Odd Then Even Math formula inside loop to generate patterns
Factorial Multiplying instead of adding in a loop
Sum of N Numbers Running total using total += i
First 5 Primes Nested loops — outer controls count, inner checks prime
Sum of First 5 Primes Same as above — add instead of print

The key idea connecting all these programs.

Every problem becomes simple when you break it into steps.

Is this number prime? → Count its divisors. If 2, yes.
Is this number perfect? → Sum its proper divisors. If equal, yes.
What is the factorial? → Multiply while counting down.

The loop just repeats those steps automatically.

Try modifying the programs.

What are the first 10 prime numbers?
What is the factorial of 10?
Sum of first 20 numbers?
Is 496 a perfect number?

Change the numbers. Run the code. Verify your answers.

That hands-on practice is where loops truly click.

If you have a question — drop it in the comments below.


Thanks for reading. Keep building.

Top comments (0)