0% found this document useful (0 votes)
4 views

Class 11 Practical Programs-Term II

Uploaded by

anirud0201
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Class 11 Practical Programs-Term II

Uploaded by

anirud0201
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Term-II-Record Programs

11.Write a menu driven program to generate the following patterns using nested loops:-

while True:
print("\nMenu:")
print("1. Print Pattern-1")
print("2. Print Pattern-2")
print("3. Exit")
choice = int(input("Enter your choice (1/2/3): "))
if choice == 1:
print("\nPattern-1:")
rows = 5
for i in range(rows, 0, -1):
for j in range(1, i + 1):
print(j, end="")
print()
elif choice == 2:
print("\nPattern-2:")
rows = 5
for i in range(1, rows + 1):
for j in range(65, 65 + i): # ASCII value of 'A' is 65
print(chr(j), end="")
print()
elif choice == 3:
print("Exiting the program.")
break
else:
print("Invalid choice! Please try again.")
12. Write a program to input the value of x and n and print the sum of the following series:

x = float(input("Enter the value of x: "))


n = int(input("Enter the value of n: "))
# Initialize the sum
series_sum = 0
# Loop to calculate the sum of the series
for i in range(n + 1):
term = ((-1) ** i) * (x ** i)
series_sum += term
print(f"The sum of the series is: {series_sum}")
13.Determine whether a given number is an Armstrong number.
num = int(input("Enter a number: "))
num_of_digits = len(str(num))
sum_of_powers = 0
temp = num
while temp > 0:
digit = temp % 10
sum_of_powers += digit ** num_of_digits
temp //= 10
if sum_of_powers == num:
print(f"{num} is an Armstrong number.")
else:
print(f"{num} is not an Armstrong number.")
14.Compute the greatest common divisor and least common multiple of two integers. The user should provide
their choice of computing either the LCM or the GCD of two integers.

import math # Import math module for GCD function


while True:
print("\nMenu:")
print("1. Compute GCD")
print("2. Compute LCM")
print("3. Exit")
choice = int(input("Enter your choice (1/2/3): "))
if choice == 1:
a = int(input("Enter the first integer: "))
b = int(input("Enter the second integer: "))
gcd = math.gcd(a, b)
print(f"The GCD of {a} and {b} is: {gcd}")
elif choice == 2:
a = int(input("Enter the first integer: "))
b = int(input("Enter the second integer: "))
gcd = math.gcd(a, b)
lcm = abs(a * b) // gcd
print(f"The LCM of {a} and {b} is: {lcm}")
elif choice == 3:
print("Exiting the program.")
break
else:
print("Invalid choice! Please try again.")
15.Count and display the number of vowels, consonants, uppercase, lowercase characters in string.
text = input("Enter a string: ")
vowels = 0
consonants = 0
uppercase = 0
lowercase = 0
vowel_set = "aeiouAEIOU"
for char in text:
if char.isalpha(): # Check if the character is a letter
if char in vowel_set: # Check if it's a vowel
vowels += 1
else: # If not a vowel, it's a consonant
consonants += 1
if char.isupper(): # Check if it's uppercase
uppercase += 1
elif char.islower(): # Check if it's lowercase
lowercase += 1
print(f"Vowels: {vowels}")
print(f"Consonants: {consonants}")
print(f"Uppercase: {uppercase}")
print(f"Lowercase: {lowercase}")
16.Input a string and determine whether it is a palindrome or not.
text = input("Enter a string: ")
# Remove spaces and convert to lowercase for accurate comparison
checked_text = text.replace(" ", "")
checked_text=checked_text.lower()
# Check if the string is equal to its reverse
if checked_text == checked_text[::-1]:
print(f'"{text}" is a palindrome.')
else:
print(f'"{text}" is not a palindrome.')

You might also like