Python Loop Questions with Answers
1. Print Even Numbers using a For Loop
Write a Python program that prints all even numbers from 1 to 20 using a for loop.
```python
for num in range(1, 21):
if num % 2 == 0:
print(num, end=" ")
```
**Output:**
```
2 4 6 8 10 12 14 16 18 20
```
2. Sum of First N Natural Numbers using a While Loop
Write a program that takes an integer N and calculates the sum of the first N natural
numbers using a while loop.
```python
N = int(input("Enter a number: "))
sum_n = 0
i=1
while i <= N:
sum_n += i
i += 1
print("Sum of first", N, "natural numbers is:", sum_n)
```
**Example Input:**
```
Enter a number: 5
```
**Output:**
```
Sum of first 5 natural numbers is: 15
```
3. Factorial of a Number using a For Loop
Write a program to calculate the factorial of a number using a for loop.
```python
num = int(input("Enter a number: "))
factorial = 1
for i in range(1, num + 1):
factorial *= i
print("Factorial of", num, "is:", factorial)
```
**Example Input:**
```
Enter a number: 5
```
**Output:**
```
Factorial of 5 is: 120
```
4. Reverse a Number using While Loop
Write a Python program that reverses a given integer using a while loop.
```python
num = int(input("Enter a number: "))
reverse = 0
while num > 0:
digit = num % 10
reverse = reverse * 10 + digit
num //= 10
print("Reversed number:", reverse)
```
**Example Input:**
```
Enter a number: 1234
```
**Output:**
```
Reversed number: 4321
```
5. Multiplication Table using a For Loop
Write a Python program to print the multiplication table of a number using a for loop.
```python
num = int(input("Enter a number: "))
for i in range(1, 11):
print(num, "x", i, "=", num * i)
```
**Example Input:**
```
Enter a number: 7
```
**Output:**
```
7x1=7
7 x 2 = 14
...
7 x 10 = 70
```
6. Count Digits in a Number using While Loop
Write a program that counts the number of digits in a given integer using a while loop.
```python
num = int(input("Enter a number: "))
count = 0
while num > 0:
count += 1
num //= 10
print("Number of digits:", count)
```
**Example Input:**
```
Enter a number: 98765
```
**Output:**
```
Number of digits: 5
```
7. Print a Pattern using Nested Loops
Write a Python program to print the following pattern using nested loops:
```
*
**
***
****
*****
```
```python
rows = 5
for i in range(1, rows + 1):
for j in range(i):
print("*", end=" ")
print()
```
**Output:**
```
*
**
***
****
*****
```
8. Find Prime Numbers in a Given Range using Nested Loops
Write a program to find all prime numbers between 10 and 50 using a for loop and a nested
loop.
```python
for num in range(10, 51):
is_prime = True
for i in range(2, num):
if num % i == 0:
is_prime = False
break
if is_prime:
print(num, end=" ")
```
**Output:**
```
11 13 17 19 23 29 31 37 41 43 47
```
9. Sum of Digits using While Loop
Write a Python program that finds the sum of digits of a given number using a while loop.
```python
num = int(input("Enter a number: "))
sum_digits = 0
while num > 0:
sum_digits += num % 10
num //= 10
print("Sum of digits:", sum_digits)
```
**Example Input:**
```
Enter a number: 1234
```
**Output:**
```
Sum of digits: 10
```
10. Generate Fibonacci Sequence using a For Loop
Write a program that prints the first N Fibonacci numbers using a for loop.
```python
N = int(input("Enter the number of terms: "))
a, b = 0, 1
for _ in range(N):
print(a, end=" ")
a, b = b, a + b
```
**Example Input:**
```
Enter the number of terms: 7
```
**Output:**
```
0112358
```