Python While Loop Practice Problems: Logic and Solutions

In the previous section we discussed problems related to while loop. Now let us take a look at some more Problems.

 

1. Sum of First N Natural Numbers

Write a Python Program to find the sum of first N natural numbers

				
					n = int(input("Enter N: "))
i = 1
sum = 0

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

print("Sum =", sum)

# Enter N: 10
# Sum = 55
				
			

2. Factorial of a Number

Write a Python Program to find the factorial of a number.

				
					n = int(input("Enter number: "))
i = 1
fact = 1

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

print("Factorial =", fact)

# Enter number: 5
# Factorial = 120

				
			

3. Fibonacci Series 

Write a Program for the Fibonacci Series

				
					n = int(input("Enter number: "))
a, b = 0, 1
i = 1

while i <= n:
    print(a, end=" ")
    c = a + b
    a = b
    b = c
    i += 1

# Enter number: 8
# 0 1 1 2 3 5 8 13 

				
			
IterationCondition (i <= 6)Print (a)c = a + ba = bb = ci += 1
Start---011
11 <= 6(True)00 + 1 = 1112
22 <= 6(True)11 + 1 = 2123
33 <= 6(True)11 + 2 = 3234
44 <= 6(True)22 + 3 = 5355
55 <= 6(True)33 + 5 = 8586
66 <= 6(True)55 + 8 = 138137
End7 <= 6(False)-----

4. Reverse a Number

Write a Program to reverse an entered number

				
					num = int(input("Enter number: "))
rev = 0

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

print("Reversed =", rev)

# Enter number: 12345
# Reversed = 54321

				
			
IterationCondition (num > 0)digit = num % 10rev = rev * 10 + digitnum //= 10
Start--012345
112345 > 0 (True)12345 mod(10) = 50 * 10 + 5 = 512345 // 10 = 1234
21234 > 0 (True)1234 mod(10) = 45 * 10 + 4 = 541234 // 10 = 123
3123 > 0 (True)123 mod(10) = 354 * 10 + 3 = 543123 // 10 = 12
412 > 0 (True)12 mod(10) = 2543 * 10 + 2 = 543212 // 10 = 1
51 > 0 (True)1 mod(10) = 15432 * 10 + 1 = 543211 // 10 = 0
End0 > 0 (False)---

5. Right Triangle Star Pattern

Write a Python Program to print the following pattern

*
**
***
****
*****

 
i = 1
while i <= 5:
    j = 1
    while j <= i:
        print("*", end="")
        j += 1
    print()
    i += 1

*
**
***
****
*****
Outer Loop (i)Condition (i <= 5)Inner Loop (j)Condition (j <= i)ActionOutput (Current Line)
11 <= 5(True)11 <= 1(True)Print *, j=2*
22 <= 1(False)Newline
22 <= 5(True)11 <= 2(True)Print *, j=2*
22 <= 2(True)Print *, j=3**
33 <= 2(False)Newline
33 <= 5(True)1, 2, 3TruePrint ***, j=4***
44 <= 3(False)
44 <= 5(True)1,2,3,4TruePrint ****, j=5****
55 <= 4(False)Newline
55 <= 5(True)1, 2, 3, 4, 5Print *****, j=6*****
66 <= 5(False)Newline
66 <= 5(False)----

6. Number Triangle

1
12
123
1234

Write a Python Program to print the above pattern

 
i = 1
while i <= 4:
    j = 1
    while j <= i:
        print(j, end="")
        j += 1
    print()
    i += 1


1
12
123
1234 
Row (i)Outer Check (i <= 4 )Col (j)Inner Check(j <= i)Action (Print)Line State
11 <= 4(True)11 <= 1(True)11
22 <= 1(False)Print Newline, i=2
22 <= 4(True)11 <= 2(True)11
22 <= 2(True)212
33 <= 2(False)Print Newline, i=3
33 <= 4(True)11 <= 3(True)11
22 <= 3(True)212
33 <= 3 (True)3123
44 <= 3(False)Print Newline, i=4
44 <= 4(True)11 <= 4(True)11
22 <= 4(True)212
33 <= 4(True)3132
44 <= 4(true)41234
55 <= 4(False)Print Newline, i=5
End5 <= 4(False)--Stop-

7. Pyramid Pattern

Write a Python program to print the pattern below

      *
    ***
  *****
*******

 
rows = 4
i = 1
while i <= rows:
space = rows - i
while space > 0:
print(" ", end="")
space -= 1
star = 1
while star <= (2 * i - 1):
print("*", end="")
star += 1
print()
i += 1 * *** ***** *******
Row (i)Spaces (4-i)Space Loop Check (>0)Star Formula (2i-1)Star Loop Check( < formula)Output Line
14 - 1 = 3Prints 3 spaces2(1) - 1 = 1Prints 1 star---*
24 - 2 = 2Prints 2 spaces2(2) - 1 = 3Prints 3 star--***
34 - 3 = 1Prints 1 spaces2(3) - 1 = 5Prints 5 star-*****
44 - 4 = 0Prints 0 spaces2(4) - 1 = 7Prints 7 star*******
Endi = 55 <= 4 (False)-Loop Terminates

8. Inverted Pattern

   *****
     ****
      ***
        **
          *

Write a Python program to print the above Pattern.

rows = 5
i = 0

while i < rows:
    space = 0
    while space < i: 
print(" ", end="")
space += 1
star = rows
while star > i:
print("*", end="") star -= 1 print() i += 1 ***** **** *** ** *

9. Diamond Pattern

        *
      ***
    *****
  *******
*********
  *******
    *****
     ***
       *

Write a Python Program for the diamond pattern

				
					rows = 5
i = 1
while i <= rows:
    space = rows - i
    while space > 0:
        print(" ", end="")
        space -= 1
    star = 1
    while star <= (2 * i - 1):
        print("*", end="")
        star += 1

    print()
    i += 1
    
# Lower Half (Inverted Pyramid)


i = rows - 1
while i >= 1:
    space = rows - i
    while space > 0:
        print(" ", end="")
        space -= 1
    star = 1
    while star <= (2 * i - 1):
        print("*", end="")
        star += 1

    print()
    i -= 1

#     *
#    ***
#   *****
#  *******
# *********
#  *******
#   *****
#    ***
#     *