📰 IT Security NachrichtenSony FE 8-14 mm F3.5: Fisheye mit Zirkular-Diagonal-Zoom im Test(10.09.2026 um 16:26 Uhr)
📰 IT Security NachrichtenKI-Modell für den Mond: Open-Source-Projekt soll Wasser-Eis finden(10.09.2026 um 16:46 Uhr)
📰 IT Security NachrichtenKirby and the World Beyond: Neues 3D-Abenteuer startet Anfang 2027(10.09.2026 um 17:07 Uhr)
📰 IT Security NachrichtenSchluss mit dem Gestank: Diese Saugroboter bringen eine echte Lösung(10.09.2026 um 17:50 Uhr)
📰 IT Security NachrichteniPhone Duo & MacBook Neo: Apple kopiert Microsoft(10.09.2026 um 18:10 Uhr)
📰 IT Security NachrichtenEuropäische Starlink-Alternative soll um hunderte Satelliten wachsen(10.09.2026 um 18:30 Uhr)
📰 IT Security NachrichtenApple-Schock: iPhone 17 Pro eingestellt, andere Preise stark erhöht(10.09.2026 um 19:07 Uhr)
📰 IT Security NachrichtenVibe Key & Pixbar: Ulanzi zeigt die Zukunft eures Schreibtischs(10.09.2026 um 19:30 Uhr)
📰 IT Security NachrichtenLuna: Diese 11 neuen Spiele verschenkt Amazon im September 2026(10.09.2026 um 20:30 Uhr)
📰 IT Security NachrichtenSony FE 8-14 mm F3.5: Fisheye mit Zirkular-Diagonal-Zoom im Test(10.09.2026 um 16:26 Uhr)
📰 IT Security NachrichtenKI-Modell für den Mond: Open-Source-Projekt soll Wasser-Eis finden(10.09.2026 um 16:46 Uhr)
📰 IT Security NachrichtenKirby and the World Beyond: Neues 3D-Abenteuer startet Anfang 2027(10.09.2026 um 17:07 Uhr)
📰 IT Security NachrichtenSchluss mit dem Gestank: Diese Saugroboter bringen eine echte Lösung(10.09.2026 um 17:50 Uhr)
📰 IT Security NachrichteniPhone Duo & MacBook Neo: Apple kopiert Microsoft(10.09.2026 um 18:10 Uhr)
📰 IT Security NachrichtenEuropäische Starlink-Alternative soll um hunderte Satelliten wachsen(10.09.2026 um 18:30 Uhr)
📰 IT Security NachrichtenApple-Schock: iPhone 17 Pro eingestellt, andere Preise stark erhöht(10.09.2026 um 19:07 Uhr)
📰 IT Security NachrichtenVibe Key & Pixbar: Ulanzi zeigt die Zukunft eures Schreibtischs(10.09.2026 um 19:30 Uhr)
📰 IT Security NachrichtenLuna: Diese 11 neuen Spiele verschenkt Amazon im September 2026(10.09.2026 um 20:30 Uhr)

🔧 Programmierung 🕛 vor 3 Monaten 13 Min Lesezeit
0

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

↗ Quelle (dev.to)
🗣️ Stimme:

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.




CODE
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")






Input: 7



Output:




CODE
7 is a Prime Number






Input: 12



Output:




CODE
12 is NOT a Prime Number






Input: 1



Output:




CODE
1 is NOT a Prime Number






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.




CODE
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")






Input: 6



Output:




CODE
6 is a Perfect Number






Input: 28



Output:




CODE
28 is a Perfect Number






Input: 10



Output:




CODE
10 is NOT a Perfect Number






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.




CODE
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






Output:




CODE
1 3 5 7 9 2 4 6 8 10






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.




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

i = 1
result = 1

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

print("Factorial of", n, "is:", result)






Input: 5



Output:




CODE
Factorial of 5 is: 120






Input: 0



Output:




CODE
Factorial of 0 is: 1






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!:




CODE
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









5. Sum of First N Numbers



Add all numbers from 1 to n.



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




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

i = 1
total = 0

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

print("Sum of first", n, "numbers:", total)






Input: 5



Output:




CODE
Sum of first 5 numbers: 15






Input: 10



Output:




CODE
Sum of first 10 numbers: 55






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.




CODE
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()






Output:




CODE
2 3 5 7 11






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.




CODE
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)






Output:




CODE
Sum of first 5 prime numbers: 28






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




CODE
i = 1

while i <= 5:
print(i)
i += 1






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.




CODE
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)






Input: 12



Output:




CODE
Count of divisors: 6






Input: 7



Output:




CODE
Count of divisors: 2






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.




CODE
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")






Input: 7



Output:




CODE
7 is a Prime Number






Input: 12



Output:




CODE
12 is NOT a Prime Number






Input: 1



Output:




CODE
1 is NOT a Prime Number






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.




CODE
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")






Input: 6



Output:




CODE
6 is a Perfect Number






Input: 28



Output:




CODE
28 is a Perfect Number






Input: 10



Output:




CODE
10 is NOT a Perfect Number






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.




CODE
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






Output:




CODE
1 3 5 7 9 2 4 6 8 10






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.




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

i = 1
result = 1

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

print("Factorial of", n, "is:", result)






Input: 5



Output:




CODE
Factorial of 5 is: 120






Input: 0



Output:




CODE
Factorial of 0 is: 1






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!:




CODE
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









13. Sum of First N Numbers



Add all numbers from 1 to n.



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




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

i = 1
total = 0

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

print("Sum of first", n, "numbers:", total)






Input: 5



Output:




CODE
Sum of first 5 numbers: 15






Input: 10



Output:




CODE
Sum of first 10 numbers: 55






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.




CODE
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()






Output:




CODE
2 3 5 7 11






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.




CODE
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)






Output:




CODE
Sum of first 5 prime numbers: 28






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.

Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
2 Quellen
T-Mobile will give you the iPhone 18 Pro for free – here’s how to preorder
1 Quelle
Asus: Erster Mini-Desktop mit Snapdragon X2 Elite - zu Mondpreisen
1 Quelle
Luna: Diese 11 neuen Spiele verschenkt Amazon im September 2026
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Python While Loop Programs — Divisors, Primes, Factorial and More

Thematisch verwandte Begriffe: Python, While, Loop, Programs · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...