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
| Iteration | Condition (i <= 6) | Print (a) | c = a + b | a = b | b = c | i += 1 |
|---|---|---|---|---|---|---|
| Start | - | - | - | 0 | 1 | 1 |
| 1 | 1 <= 6(True) | 0 | 0 + 1 = 1 | 1 | 1 | 2 |
| 2 | 2 <= 6(True) | 1 | 1 + 1 = 2 | 1 | 2 | 3 |
| 3 | 3 <= 6(True) | 1 | 1 + 2 = 3 | 2 | 3 | 4 |
| 4 | 4 <= 6(True) | 2 | 2 + 3 = 5 | 3 | 5 | 5 |
| 5 | 5 <= 6(True) | 3 | 3 + 5 = 8 | 5 | 8 | 6 |
| 6 | 6 <= 6(True) | 5 | 5 + 8 = 13 | 8 | 13 | 7 |
| End | 7 <= 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
| Iteration | Condition (num > 0) | digit = num % 10 | rev = rev * 10 + digit | num //= 10 |
|---|---|---|---|---|
| Start | - | - | 0 | 12345 |
| 1 | 12345 > 0 (True) | 12345 mod(10) = 5 | 0 * 10 + 5 = 5 | 12345 // 10 = 1234 |
| 2 | 1234 > 0 (True) | 1234 mod(10) = 4 | 5 * 10 + 4 = 54 | 1234 // 10 = 123 |
| 3 | 123 > 0 (True) | 123 mod(10) = 3 | 54 * 10 + 3 = 543 | 123 // 10 = 12 |
| 4 | 12 > 0 (True) | 12 mod(10) = 2 | 543 * 10 + 2 = 5432 | 12 // 10 = 1 |
| 5 | 1 > 0 (True) | 1 mod(10) = 1 | 5432 * 10 + 1 = 54321 | 1 // 10 = 0 |
| End | 0 > 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) | Action | Output (Current Line) |
|---|---|---|---|---|---|
| 1 | 1 <= 5(True) | 1 | 1 <= 1(True) | Print *, j=2 | * |
| 2 | 2 <= 1(False) | Newline | |||
| 2 | 2 <= 5(True) | 1 | 1 <= 2(True) | Print *, j=2 | * |
| 2 | 2 <= 2(True) | Print *, j=3 | ** | ||
| 3 | 3 <= 2(False) | Newline | |||
| 3 | 3 <= 5(True) | 1, 2, 3 | True | Print ***, j=4 | *** |
| 4 | 4 <= 3(False) | ||||
| 4 | 4 <= 5(True) | 1,2,3,4 | True | Print ****, j=5 | **** |
| 5 | 5 <= 4(False) | Newline | |||
| 5 | 5 <= 5(True) | 1, 2, 3, 4, 5 | Print *****, j=6 | ***** | |
| 6 | 6 <= 5(False) | Newline | |||
| 6 | 6 <= 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 |
|---|---|---|---|---|---|
| 1 | 1 <= 4(True) | 1 | 1 <= 1(True) | 1 | 1 |
| 2 | 2 <= 1(False) | Print Newline, i=2 | |||
| 2 | 2 <= 4(True) | 1 | 1 <= 2(True) | 1 | 1 |
| 2 | 2 <= 2(True) | 2 | 12 | ||
| 3 | 3 <= 2(False) | Print Newline, i=3 | |||
| 3 | 3 <= 4(True) | 1 | 1 <= 3(True) | 1 | 1 |
| 2 | 2 <= 3(True) | 2 | 12 | ||
| 3 | 3 <= 3 (True) | 3 | 123 | ||
| 4 | 4 <= 3(False) | Print Newline, i=4 | |||
| 4 | 4 <= 4(True) | 1 | 1 <= 4(True) | 1 | 1 |
| 2 | 2 <= 4(True) | 2 | 12 | ||
| 3 | 3 <= 4(True) | 3 | 132 | ||
| 4 | 4 <= 4(true) | 4 | 1234 | ||
| 5 | 5 <= 4(False) | Print Newline, i=5 | |||
| End | 5 <= 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 |
|---|---|---|---|---|---|
| 1 | 4 - 1 = 3 | Prints 3 spaces | 2(1) - 1 = 1 | Prints 1 star | ---* |
| 2 | 4 - 2 = 2 | Prints 2 spaces | 2(2) - 1 = 3 | Prints 3 star | --*** |
| 3 | 4 - 3 = 1 | Prints 1 spaces | 2(3) - 1 = 5 | Prints 5 star | -***** |
| 4 | 4 - 4 = 0 | Prints 0 spaces | 2(4) - 1 = 7 | Prints 7 star | ******* |
| End | i = 5 | 5 <= 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
# *
# ***
# *****
# *******
# *********
# *******
# *****
# ***
# *