Total Pageviews

Saturday, April 18, 2020

WHILE LOOP IN PYTHON

1. BASIC CLICK
2. Strong Number in Python CLICK
3. WAP to calculate the sum and product of digit of a number. CLICK



📘 while Loop in Python (Explanation + 20 Programs)


🎯 What is a while loop?

A while loop is used to repeat a block of code as long as a condition is True.


📌 Definition

A while loop executes a set of statements repeatedly until the given condition becomes False.


🧠 Syntax

while condition:
# code block

⚡ How it works

  1. Check condition
  2. If True → execute code
  3. Update variable
  4. Repeat
  5. Stop when condition becomes False

📘 Example

i = 1

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

📌 Output

1
2
3
4
5

🔥 Difference: while vs for

Featurewhile loopfor loop
ConditionYesNo (range-based)
UsageUnknown iterationsKnown iterations
ControlManualAutomatic

🐍 20 BASIC PROGRAMS USING while LOOP


1. Print 1 to 10

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

2. Print 10 to 1

i = 10
while i >= 1:
print(i)
i -= 1

3. Even Numbers (1–20)

i = 2
while i <= 20:
print(i)
i += 2

4. Odd Numbers (1–20)

i = 1
while i <= 20:
print(i)
i += 2

5. Sum 1 to 10

i = 1
sum = 0

while i <= 10:
sum += i
i += 1

print(sum)

6. Factorial

n = 5
fact = 1
i = 1

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

print(fact)

7. Multiplication Table

n = 5
i = 1

while i <= 10:
print(n, "x", i, "=", n*i)
i += 1

8. Reverse Number

n = 123
rev = 0

while n > 0:
digit = n % 10
rev = rev * 10 + digit
n //= 10

print(rev)

9. Palindrome Number

n = 121
temp = n
rev = 0

while n > 0:
rev = rev * 10 + n % 10
n //= 10

if temp == rev:
print("Palindrome")
else:
print("Not Palindrome")

10. Sum of Digits

n = 123
sum = 0

while n > 0:
sum += n % 10
n //= 10

print(sum)

11. Count Digits

n = 12345
count = 0

while n > 0:
count += 1
n //= 10

print(count)

12. Fibonacci Series

a, b = 0, 1
i = 1

while i <= 10:
print(a)
a, b = b, a + b
i += 1

13. Print Characters

s = "Python"
i = 0

while i < len(s):
print(s[i])
i += 1

14. Find Largest Number

arr = [10, 50, 20, 80]
i = 0
max = arr[0]

while i < len(arr):
if arr[i] > max:
max = arr[i]
i += 1

print(max)

15. Find Smallest Number

arr = [10, 50, 20, 80]
i = 0
min = arr[0]

while i < len(arr):
if arr[i] < min:
min = arr[i]
i += 1

print(min)

16. Print List Elements

arr = [1, 2, 3, 4]
i = 0

while i < len(arr):
print(arr[i])
i += 1

17. Sum of List

arr = [10, 20, 30]
i = 0
sum = 0

while i < len(arr):
sum += arr[i]
i += 1

print(sum)

18. Count Even Numbers

i = 1
count = 0

while i <= 50:
if i % 2 == 0:
count += 1
i += 1

print(count)

19. Simple Guess Game

secret = 5
guess = 0

while guess != secret:
guess = int(input("Enter number: "))

print("Correct!")

20. Infinite Loop (Example)

# while True:
# print("Hello")

📌 Summary

  • while loop runs until condition becomes False
  • Used when iterations are unknown
  • Requires manual update of variable
  • Very useful in number problems and logic building

🧠 Important Questions

Short Questions

  1. What is while loop?
  2. Write syntax of while loop.
  3. Difference between while and for loop.
  4. What is infinite loop?
  5. Give one example.

Long Questions

  1. Explain while loop with example.
  2. Write program for factorial using while loop.
  3. Write program for reverse number using while loop.
  4. Explain Fibonacci series using while loop.
  5. Write 10 programs using while loop. 

No comments:

Post a Comment