1. Write a program to find sum of odd numbers between 100 and 50.
sum_odd = 0
for i in range(51, 101):
if i % 2 != 0:
sum_odd += i
print("Sum of odd numbers between 50 and 100 is:", sum_odd)
Output:
Sum of odd numbers between 50 and 100 is: 1875
2. Write a program to print even numbers within the range entered by the user.
start = int(input("Enter start of range: "))
end = int(input("Enter end of range: "))
for i in range(start, end+1):
if i % 2 == 0:
print(i, end=" ")
Output:
Example Input:
Enter start of range: 5
Enter end of range: 15
Output:
6 8 10 12 14
3. Write a program to print square of multiples of 3 till n number of terms, where n is entered by the user.
n = int(input("Enter number of terms: "))
num = 3
for i in range(1, n+1):
print(num**2, end=" ")
num += 3
Output:
Example Input:
Enter number of terms: 5
Output:
9 36 81 144 225
4. Write a program to find sum of the following series: S=x+x^2+x^3+......+n terms.
x = int(input("Enter value of x: "))
n = int(input("Enter number of terms: "))
s = 0
for i in range(1, n+1):
s += x**i
print("Sum of series is:", s)
Output:
Example Input:
Enter value of x: 2
Enter number of terms: 4
Output:
Sum of series is: 30
5. Write a program to enter a string, find its length, and print it in the reverse order.
s = input("Enter a string: ")
length = 0
for ch in s:
length += 1
print("Length of string is:", length)
print("Reverse of string is:", s[::-1])
Output:
Example Input:
Enter a string: hello
Output:
Length of string is: 5
Reverse of string is: olleh
6. Enter a string and count number of vowels in it.
s = input("Enter a string: ")
vowels = "aeiouAEIOU"
count = 0
for ch in s:
if ch in vowels:
count += 1
print("Number of vowels:", count)
Output:
Example Input:
Enter a string: computer
Output:
Number of vowels: 3
7. Write a code to enter a string and print elements lying at even positions in the string.
s = input("Enter a string: ")
for i in range(len(s)):
if i % 2 == 0:
print(s[i], end=" ")
Output:
Example Input:
Enter a string: python
Output:
p t o
8. Write a code to enter a sentence and count number of times the word 'the' comes in it.
s = input("Enter a sentence: ")
words = s.split()
count = 0
for w in words:
if w.lower() == "the":
count += 1
print("Number of times 'the' comes:", count)
Output:
Example Input:
Enter a sentence: The sun sets in the west
Output:
Number of times 'the' comes: 2
9. Write a code to enter a sentence and toggle the cases in it.
s = input("Enter a sentence: ")
result = ""
for ch in s:
if ch.islower():
result += ch.upper()
elif ch.isupper():
result += ch.lower()
else:
result += ch
print("Toggled string:", result)
Output:
Example Input:
Enter a sentence: PyThoN 123
Output:
pYtHOn 123
10. Write a code to enter a sentence and make its copy in such a way that all the digits are replaced by @
symbol.
s = input("Enter a sentence: ")
result = ""
for ch in s:
if ch.isdigit():
result += "@"
else:
result += ch
print("Modified string:", result)
Output:
Example Input:
Enter a sentence: I have 2 dogs and 3 cats
Output:
I have @ dogs and @ cats
11. Write a menu driven program which performs the following operations: a. count number of blank
spaces in it. b. count number of consonants in it.
s = input("Enter a string: ")
print("Menu:")
print("1. Count spaces")
print("2. Count consonants")
choice = int(input("Enter your choice: "))
if choice == 1:
count = 0
for ch in s:
if ch == " ":
count += 1
print("Number of spaces:", count)
elif choice == 2:
vowels = "aeiouAEIOU"
count = 0
for ch in s:
if ch.isalpha() and ch not in vowels:
count += 1
print("Number of consonants:", count)
Output:
Example Input:
Enter a string: Hello World
Menu:
1. Count spaces
2. Count consonants
Enter your choice: 1
Output:
Number of spaces: 1
If choice=2:
Number of consonants: 7
12. Write a program to enter a string and print it in the reverse triangular pattern.
s = input("Enter a string: ")
for i in range(len(s), 0, -1):
print(s[:i])
Output:
Example Input:
Enter a string: hello
Output:
hello
hell
hel
he
h