Question: Write a program to find the sum of the first n
natural numbers.
Example Input:n = 5
Example Output:15
Solution:
n = 5
sum = 0
for i in range(1, n + 1):
sum += i
print(sum)
Explanation: The loop iterates from 1 to n
, adding each number to sum
.
Question: Write a program to find the factorial of a number n
.
Example Input:n = 4
Example Output:24
Solution:
n = 4
factorial = 1
for i in range(1, n + 1):
factorial *= i
print(factorial)
Explanation: The loop multiplies factorial
by each number from 1 to n
.
Question: Write a program to print the multiplication table of a number n
.
Example Input:n = 3
Example Output:
3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
...
3 x 10 = 30
Solution:
n = 3
for i in range(1, 11):
print(f"{n} x {i} = {n * i}")
Explanation: The loop prints the product of n
and each number from 1 to 10.
Question: Write a program to reverse a given string.
Example Input:s = "hello"
Example Output:olleh
Solution:
s = "hello"
reversed_s = ""
for char in s:
reversed_s = char + reversed_s
print(reversed_s)
Explanation: The loop prepends each character of s
to reversed_s
.
Question: Write a program to count the number of digits in a number n
.
Example Input:n = 12345
Example Output:5
Solution:
n = 12345
count = 0
while n != 0:
n //= 10
count += 1
print(count)
Explanation: The loop divides n
by 10 until n
becomes 0, counting the divisions.
Question: Write a program to print the first n
terms of the Fibonacci sequence.
Example Input:n = 5
Example Output:0 1 1 2 3
Solution:
n = 5
a, b = 0, 1
for _ in range(n):
print(a, end=" ")
a, b = b, a + b
Explanation: The loop prints a
and updates a
and b
to the next Fibonacci numbers.
Question: Write a program to find the sum of digits of a number n
.
Example Input:n = 123
Example Output:6
Solution:
n = 123
sum = 0
while n > 0:
sum += n % 10
n //= 10
print(sum)
Explanation: The loop extracts the last digit of n
and adds it to sum
.
Question: Write a program to check if a given string is a palindrome.
Example Input:s = "racecar"
Example Output:True
Solution:
s = "racecar"
is_palindrome = True
for i in range(len(s) // 2):
if s[i] != s[-(i + 1)]:
is_palindrome = False
break
print(is_palindrome)
Explanation: The loop compares characters from the start and end of the string.
Question: Write a program to find the largest number in a list.
Example Input:lst = [3, 1, 4, 1, 5, 9]
Example Output:9
Solution:
lst = [3, 1, 4, 1, 5, 9]
largest = lst[0]
for num in lst:
if num > largest:
largest = num
print(largest)
Explanation: The loop updates largest
if it finds a larger number in lst
.
Question: Write a program to find the second largest number in a list.
Example Input:lst = [3, 1, 4, 1, 5, 9]
Example Output:5
Solution:
lst = [3, 1, 4, 1, 5, 9]
largest = second_largest = float('-inf')
for num in lst:
if num > largest:
second_largest = largest
largest = num
elif num > second_largest and num != largest:
second_largest = num
print(second_largest)
Explanation: The loop updates largest
and second_largest
appropriately.
Question: Write a program to count the number of vowels in a given string.
Example Input:s = "hello"
Example Output:2
Solution:
s = "hello"
vowels = "aeiou"
count = 0
for char in s:
if char in vowels:
count += 1
print(count)
Explanation: The loop checks if each character is a vowel and increments count
.
Question: Write a program to find the sum of even numbers from 1 to n
.
Example Input:n = 10
Example Output:30
Solution:
n = 10
sum = 0
for i in range(2, n + 1, 2):
sum += i
print(sum)
Explanation: The loop iterates over even numbers and adds them to sum
.
Question: Write a program to count the number of words in a given string.
Example Input:s = "hello world"
Example Output:2
Solution:
s = "hello world"
count = 1
for char in s:
if char == ' ':
count += 1
print(count)
Explanation: The loop counts spaces and adds 1 to get the number of words.