Solution on Loops
๐งฎ 1. Print Numbers from 1 to 10
Problem:
Print numbers from 1 to 10 using a for
loop.
Example Output:
1
2
3
4
5
6
7
8
9
10
Solution:
for i in range(1, 11):
print(i)
Explanation:
range(1, 11)
generates numbers from 1 to 10.
The for
loop prints each number one by one.
๐ข 2. Sum of First N Natural Numbers
Problem:
Calculate the sum of numbers from 1 to N.
Example Input:
N = 5
Example Output:
15
Solution:
N = 5
total = 0
for i in range(1, N + 1):
total += i
print(total)
Explanation:
We initialize a variable total
and keep adding each number in every loop cycle.
โญ 3. Print Each Character in a String
Problem:
Print each character of a string using a for
loop.
Example Input:
Python
Example Output:
P
y
t
h
o
n
Solution:
word = "Python"
for char in word:
print(char)
Explanation:
Strings are iterable โ the loop automatically picks each character one by one.
๐งพ 4. Print Even Numbers from 1 to 20
Problem:
Print only even numbers between 1 and 20.
Example Output:
2 4 6 8 10 12 14 16 18 20
Solution:
for i in range(2, 21, 2):
print(i, end=" ")
Explanation:
The third argument in range(start, stop, step)
controls the increment.
Here, we step by 2 to get only even numbers.
๐ฌ 5. Print List Elements Using Loop
Problem:
Display all elements from a list using a loop.
Example Input:
fruits = ["apple", "banana", "cherry"]
Example Output:
apple
banana
cherry
Solution:
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
Explanation:
Looping over a list gives you each element directly โ no index needed.
๐ 6. Reverse Counting from 10 to 1
Problem:
Print numbers from 10 down to 1.
Example Output:
10 9 8 7 6 5 4 3 2 1
Solution:
for i in range(10, 0, -1):
print(i, end=" ")
Explanation:
Using a negative step value in range()
helps count backward.
โณ 7. Print Multiplication Table of a Number
Problem:
Take a number and print its multiplication table up to 10.
Example Input:
n = 3
Example Output:
3 x 1 = 3
3 x 2 = 6
...
3 x 10 = 30
Solution:
n = 3
for i in range(1, 11):
print(f"{n} x {i} = {n * i}")
Explanation:
We multiply n
by each i
in range 1โ10 and print the result.
๐ง 8. Find the Factorial of a Number
Problem:
Find factorial of a number (e.g., 5! = 1ร2ร3ร4ร5).
Example Input:
5
Example Output:
120
Solution:
n = 5
fact = 1
for i in range(1, n + 1):
fact *= i
print(fact)
Explanation:
Multiply all numbers from 1 to n sequentially to find the factorial.
๐ 9. Print Digits of a Number (Using while loop)
Problem:
Display each digit of a number separately.
Example Input:
1234
Example Output:
4
3
2
1
Solution:
num = 1234
while num > 0:
digit = num % 10
print(digit)
num //= 10
Explanation:
Use modulo %
to extract the last digit and integer division //
to remove it.
๐ฎ 10. FizzBuzz (Classic Loop Problem)
Problem:
Print numbers 1โ20 but:
- Print โFizzโ if divisible by 3
- Print โBuzzโ if divisible by 5
- Print โFizzBuzzโ if divisible by both
Example Output:
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
...
Solution:
for i in range(1, 21):
if i % 3 == 0 and i % 5 == 0:
print("FizzBuzz")
elif i % 3 == 0:
print("Fizz")
elif i % 5 == 0:
print("Buzz")
else:
print(i)
Explanation:
Conditions are checked in order.
If both 3 and 5 divide the number, print "FizzBuzz"
first โ otherwise the correct single case.
๐งฎ 11. Sum of Even Numbers in a List
Problem: Calculate the sum of all even numbers in a given list.
Example Input:
numbers = [1, 4, 7, 10, 12]
Output:
26
numbers = [1, 4, 7, 10, 12]
total = 0
for num in numbers:
if num % 2 == 0:
total += num
print(total)
Explanation: Loop over each number; if divisible by 2, add to total
.
๐ข 12. Count Vowels in a String
Problem: Count the number of vowels in a string.
Input: "Python is fun"
Output: 4
text = "Python is fun"
vowels = "aeiouAEIOU"
count = 0
for char in text:
if char in vowels:
count += 1
print(count)
Explanation: Check each character; increment counter if itโs a vowel.
โญ 13. Print Numbers Divisible by 4 and 6
Problem: Print numbers from 1 to 50 divisible by both 4 and 6.
Output:
12 24 36 48
for i in range(1, 51):
if i % 4 == 0 and i % 6 == 0:
print(i, end=" ")
Explanation: Use modulo operator to check divisibility by both numbers.
๐งพ 14. Cumulative Sum of List
Problem: Print cumulative sum at each step from a list.
Input: [2, 5, 3, 7]
Output: 2 7 10 17
numbers = [2, 5, 3, 7]
total = 0
for num in numbers:
total += num
print(total, end=" ")
Explanation: Add each number to a running total and print progressively.
๐ 15. Print Multiples of 3 and 5
Problem: Print numbers between 1โ50 divisible by 3 or 5.
Output:
3 5 6 9 10 12 ... 45 48 50
for i in range(1, 51):
if i % 3 == 0 or i % 5 == 0:
print(i, end=" ")
Explanation: Check each number for divisibility by 3 or 5 using %
.
โณ 16. Reverse a String
Problem: Print a string in reverse.
Input: "Python"
Output: nohtyP
text = "Python"
reversed_text = ""
for char in text:
reversed_text = char + reversed_text
print(reversed_text)
Explanation: Prepend each character to the result string to reverse it.
๐ง 17. Find Numbers with Even Digits
Problem: Print numbers from 1โ100 that have an even number of digits.
Output:
10 11 12 ... 99
for i in range(1, 101):
if len(str(i)) % 2 == 0:
print(i, end=" ")
Explanation: Convert each number to string; check if its length is even.
๐ 18. Find the Maximum Number in a List
Problem: Find the largest number in a list.
Input: [5, 12, 3, 21, 7]
Output: 21
numbers = [5, 12, 3, 21, 7]
max_num = numbers[0]
for num in numbers:
if num > max_num:
max_num = num
print(max_num)
Explanation: Compare each element with max_num
and update if larger.
๐ข 19. Count Digits and Letters in a String
Problem: Count letters and digits separately in a string.
Input: "Python3.8"
Output:
Letters: 6
Digits: 2
text = "Python3.8"
letters = digits = 0
for char in text:
if char.isalpha():
letters += 1
elif char.isdigit():
digits += 1
print("Letters:", letters)
print("Digits:", digits)
Explanation: Use isalpha()
and isdigit()
to categorize each character.
๐ฎ 20. Fibonacci Numbers Less Than N
Problem: Print all Fibonacci numbers less than a given N.
Input: N = 20
Output: 0 1 1 2 3 5 8 13
N = 20
a, b = 0, 1
while a < N:
print(a, end=" ")
a, b = b, a + b
Explanation: Generate Fibonacci numbers in a while
loop until exceeding N.
๐งฎ 21. Print Odd Numbers in a List
Problem: Print all odd numbers from a given list.
Example Input:
numbers = [2, 7, 4, 9, 12, 15]
Output:
7 9 15
numbers = [2, 7, 4, 9, 12, 15]
for num in numbers:
if num % 2 != 0:
print(num, end=" ")
Explanation: Check if a number is not divisible by 2 using %
.
๐ข 22. Count Uppercase and Lowercase Letters
Problem: Count the number of uppercase and lowercase letters in a string.
Input: "Python Is Fun"
Output:
Uppercase: 3
Lowercase: 8
text = "Python Is Fun"
upper = lower = 0
for char in text:
if char.isupper():
upper += 1
elif char.islower():
lower += 1
print("Uppercase:", upper)
print("Lowercase:", lower)
Explanation: Use isupper()
and islower()
for classification.
โญ 23. Print Squares of Numbers
Problem: Print squares of numbers from 1 to N.
Input: N = 5
Output:
1 4 9 16 25
N = 5
for i in range(1, N + 1):
print(i**2, end=" ")
Explanation: Multiply each number by itself using the exponent operator **
.
๐งพ 24. Print Cubes of Numbers in a List
Problem: Print cubes of numbers from a given list.
Input: [1, 2, 3, 4]
Output:
1 8 27 64
numbers = [1, 2, 3, 4]
for num in numbers:
print(num**3, end=" ")
Explanation: Use num**3
to calculate the cube of each number.
๐ 25. Print Numbers Divisible by 7 but Not by 5
Problem: Print numbers from 1 to 100 divisible by 7 but not by 5.
Output:
7 14 21 28 ... 91 98
for i in range(1, 101):
if i % 7 == 0 and i % 5 != 0:
print(i, end=" ")
Explanation: Use two conditions combined with and
and !=
.
โณ 26. Print Factorials of Numbers in a List
Problem: Print factorial of each number in a given list.
Input: [3, 4, 5]
Output:
6 24 120
numbers = [3, 4, 5]
for num in numbers:
fact = 1
for i in range(1, num + 1):
fact *= i
print(fact, end=" ")
Explanation: Use a loop to calculate the factorial of each number.
๐ง 27. Count Words in a Sentence
Problem: Count the number of words in a given sentence.
Input: "Python is fun to learn"
Output: 5
sentence = "Python is fun to learn"
words = 0
for char in sentence:
if char == " ":
words += 1
words += 1
print(words)
Explanation: Count spaces between words; add 1 to get total words.
๐ 28. Print Prime Numbers from 1 to N
Problem: Print all prime numbers from 1 to N.
Input: N = 20
Output:
2 3 5 7 11 13 17 19
N = 20
for num in range(2, N + 1):
is_prime = True
for i in range(2, num):
if num % i == 0:
is_prime = False
break
if is_prime:
print(num, end=" ")
Explanation: A number is prime if it has no divisors other than 1 and itself.
๐ข 29. Sum of Digits of Numbers in a List
Problem: Print sum of digits for each number in a list.
Input: [12, 34, 56]
Output:
3 7 11
numbers = [12, 34, 56]
for num in numbers:
total = 0
temp = num
while temp > 0:
total += temp % 10
temp //= 10
print(total, end=" ")
Explanation: Use modulo %
to get digits and integer division //
to remove them.
๐ฎ 30. Check Palindrome Numbers in a List
Problem: Print all numbers from a list that are palindromes.
Input: [121, 123, 454, 567, 787]
Output:
121 454 787
numbers = [121, 123, 454, 567, 787]
for num in numbers:
if str(num) == str(num)[::-1]:
print(num, end=" ")
Explanation: Convert the number to a string and check if it reads the same forward and backward.