Hello friend, in today’s blog I will show you 10 problems of while loop by solving them. I have divided these questions into categories easy, intermediate and hard. If you want to improve your problem solving skills and logic building skills, then you have to do variety of questions.
1. Question: Write a program to print numbers from 10 tp 15 using a while loop.
Expected Output:- 10 11 12 13 14 15
# Initialize the starting number
num = 10
# Use a while loop to print numbers from 10 to 15
while num <= 15:
print(num)
num += 1 # Increment the number by 1 in each iteration
Output:-
10
11
12
13
14
15
2. Question: Write a program to print the cubes of numbers from 1 to 5.
Expected Output:- 1 8 27 64 125
# Use a for loop to print the cubes of numbers from 1 to 5
for num in range(1, 6):
cube = num ** 3 # Calculate the cube of the number
print(f"The cube of {num} is {cube}")
Output:-
The cube of 1 is 1
The cube of 2 is 8
The cube of 3 is 27
The cube of 4 is 64
The cube of 5 is 125
3. Question: Write a program to print all odd number from 1 to 10.
Expected Output:- 1 3 5 7 9
# Use a for loop to print all odd numbers from 1 to 10
for num in range(1, 11):
if num % 2 != 0: # Check if the number is odd
print(num)
Output:-
1
3
5
7
9
4. Question: Write a program to calculate the product of numbers from 1 to 5.
Expected Output:- Product = 120
# Initialize a variable to store the product
product = 1
# Use a for loop to calculate the product of numbers from 1 to 5
for num in range(1, 6):
product *= num # Multiply the current number with the product
# Print the final product
print("The product of numbers from 1 to 5 is:", product)
Output:-
The product of numbers from 1 to 5 is: 120
5. Question: Write a program to reverse each word in the sentence “Hello World” using a while loop.
Expected Output:- olleH dlroW
# Given sentence
sentence = "Hello World"
# Split the sentence into words
words = sentence.split()
# Initialize an empty list to store the reversed words
reversed_words = []
# Index to iterate through the list of words
i = 0
# Loop through each word in the list using a while loop
while i < len(words):
# Reverse each word and append to the reversed_words list
reversed_words.append(words[i][::-1])
i += 1
# Join the reversed words into a sentence
reversed_sentence = ' '.join(reversed_words)
# Output the reversed sentence
print(reversed_sentence)
Output:-
olleH dlroW
6. Question: Write a program to count the number of consonants in the word “learning”. Input learning
Expected Output:- 5
# Given word
word = "learning"
# Initialize a variable to count the consonants
consonant_count = 0
# Define the vowels
vowels = "aeiou"
# Loop through each letter in the word
for letter in word:
# Check if the letter is a consonant
if letter.isalpha() and letter.lower() not in vowels:
consonant_count += 1
# Output the count of consonants
print("Number of consonants:", consonant_count)
Output:-
Number of consonants: 5
7. Question: Write a program to print the first 5 multiple of 3.
Expected Output:- 3 6 9 12 15
# Number to find multiples of
number = 3
# Initialize a counter to track the first 5 multiples
count = 1
# Loop to print the first 5 multiples of 3
while count <= 5:
# Calculate and print the current multiple
print(number * count)
# Increment the counter
count += 1
Output:-
3
6
9
12
15
8. Question: Write a program to calculate 3 to the power of 4.
Input: Base = 3, Exponent = 4
Expected Output:- 81
# Given base and exponent
base = 3
exponent = 4
# Calculate base raised to the power of exponent
result = base ** exponent
# Output the result
print(f"{base} to the power of {exponent} is: {result}")
Output:-
3 to the power of 4 is: 81
9. Question: Write a program to check if a given number, such as 16, is a perfect square.
Input: 16
Expected Output:- 81
import math
# Given number
number = 16
# Check if the square root of the number is an integer
sqrt = math.sqrt(number)
if sqrt.is_integer():
print(f"{number} is a perfect square.")
else:
print(f"{number} is not a perfect square.")
Output:-
16 is a perfect square.
10. Question: Write a program to count occurrences of the character “s” in the string “success”.
Input: success
Expected Output:- 3
# Given string
word = "success"
# Count the occurrences of the character 's'
count_s = word.count('s')
# Output the result
print(f"The character 's' appears {count_s} times in the word '{word}'.")
Output:-
The character 's' appears 3 times in the word 'success'.