COMPUTER SCIENCE
CLASS-XII
HOLIDAY HOMEWORK
MOHD.ALFAIZ
CLASS XII-B
Program 1 WAP to read a number n
and print n2 , n3 and n4
Solution :
num = int(input("enter the num"))
square = num**2
cube = num**3
power_four = num**4
print(square)
print(cube)
print(power_four)
Program 2 WAP to print the table of a
given number in the format 2 X 1 = 2.
Solution :
num = int(input("enter the num"))
i=1
while i <= 10 :
print(num , "x" , i , "=" , num*i)
i += 1
Program 3 WAP to input a single digit n
and print a 3-digit number created as .
(Hint- if n=7 then it should display 789
Solution :
num = int(input("enter the num"))
print(f"{num}{num+1}{num+2}")
Program 4 WAP to make a menu driven list for perimeter and area
of different shapes
Solution :
a = input("""choose the shape -->
1.Square
2.Circle
3.Triangle[Equilateral]
4.Rectangle
choose : """)
if a == "Square" :
side = int(input("measurement of side in cms : "))
perimeter = 4*side
area = side**2
print("perimeter : ",perimeter)
print("area : ",area)
elif a == "Circle" :
radius = int(input("measurement of radius in cms : "))
pi = 3.14
circumference = 2*pi*radius
area = pi*radius**2
print("Circumference : ",circumference)
print("area : ",area)
elif a == "Triangle" :
base = int(input("measurement of base in cms : "))
height = int(input("measurement of height in cms : "))
side = int(input("measurement of side in cms : "))
perimeter = side + side + side
area = 1/2*(base*height)
print("perimeter : ",perimeter)
print("area : ",area)
elif a == "Rectangle" :
length = int(input("measurement of Length in cms : "))
width = int(input("measurement of width in cms : "))
perimeter = (length+width)*2
area = length*width
print("perimeter : ",perimeter)
print("area : ",area)
else :
print("invalid shape")
Program 5 WAP to read three numbers and print them in
ascending order.
Solution :
a = int(input("enter the number 1 : "))
b = int(input("enter the number 2 : "))
c = int(input("enter the number 3 : "))
if a <= b and b <= c :
print( a , b , c)
elif a <= c and c <= b :
print( a, c, b)
elif b <= a and a <= c :
print(b , a ,c )
elif b <= c and c <= a :
print(b,c,a)
elif c <= b and b <= a :
print(c,b,a)
elif c <= a and a <= b :
print(c,a,b)
else :
print("invalid number")
Program 6 WAP to print factorial of a
given number.
Solution :
n = int(input("Enter a number: "))
f=1
i=1
while i <= n:
f=f*i
i=i+1
print("Factorial is", f)
Program 7 WAP to check that whether
a given number is prime or not
Solution :
a = int(input("enter the number : "))
if a % 2 != 0 :
print("it is a prime number")
else :
print("it is not a prime number")
Program 8 WAP to print Fibonacci
series of first 20 elements.
Solution :
a=0
b=1
count = 0
while count < 20 :
print(a , end = " ")
a , b = b , a+b
count +=1
Program 9 first 20 Mersenne numbers.
(Hint: Mersenne numbers are 2 n -1)
Solution :
n = int(input("enter the power"))
i=1
while i <= 20 :
print(2**n - 1)
i+=1
n+=1
Program 10 WAP to print the
following patterns
(a)
Solution :
n=4
for i in range(1, n + 1):
num = 1
for j in range(i):
print(num, end="")
num += 2
print()
(b)
Solution :
for i in range(4, 0, -1):
for j in range(4, 4 - i, -1):
print(j, end="")
print()
Program 11 WAP to print the
following series: (Hint: Input the values
of x and n)
(a) 1+x+x2+x3+x4…….xn
Solution :
x = int(input("Enter the value of x: "))
n = int(input("Enter the value of n: "))
sum = 0
i=0
while i <= n:
sum += x ** i
i += 1
print("Sum of the series is:", sum)
(b) 1−x+x2−x3+x4−x5+… up to xn
Solution :
x = int(input("Enter the value of x: "))
n = int(input("Enter the value of n: "))
sum = 0
i=0
while i <= n:
term = x ** i
if i % 2 == 1:
term = -term
sum += term
i += 1
print("Sum of the series is:", sum)
Program 12 WAP to check that
whether a given string is palindrome or
not.
Solution :
s = input("Enter a string: ")
s_clean = s.replace(" ", "").lower()
if s_clean == s_clean[::-1]:
print("The string is a palindrome.")
else:
print("The string is not palindrome.")
Program 13 WAP to reverse words in
a given String in Python
Solution:
sentence = input("Enter a sentence: ")
words = sentence.split()
words.reverse()
reversed_sentence = " ".join(words)
print("Reversed words:",
reversed_sentence)
Program 14 WAP to count number
of vowels in given string
Solution :
s = input("Enter a string: ").lower()
vowels = "aeiou"
count = 0
for char in s:
if char in vowels:
count += 1
print("Number of vowels:", count)
Program 15 WAP to swap commas
and dots in a given string
Solution :
s = input("Enter a string: ")
s = s.replace(',', '<comma>')
s = s.replace('.', ',')
s = s.replace('<comma>', '.')
print("Swapped string:", s)