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

CS Assignment

The document contains 27 programming problems and their solutions in Python. Each problem solution is contained within a function. The problems cover a range of concepts like lists, strings, arithmetic, conditional statements etc. Overall the document provides examples of solving common programming problems using functions.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

CS Assignment

The document contains 27 programming problems and their solutions in Python. Each problem solution is contained within a function. The problems cover a range of concepts like lists, strings, arithmetic, conditional statements etc. Overall the document provides examples of solving common programming problems using functions.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

CLASS

XII
SECTION-D

2024-2025

ROLL NO.:22

COMPUTER
SCIENCE ASSIGNMENT

PROGRAMMED BY
YUUVANESH S
1. Write function to display(l) to display all nos.
divisible by 5 in the given list.(return).
print("Yuuvanesh S XII-D RollNo.22")
def display(l):
t=[]
for i in l:
if i%5==0:
t.append(i)
return t
l=[]
n=int(input("Enter no. of element:"))
for i in range(n):
l.append(int(input("Enter element:")))
print(display(l))

2. Return all the even numbers using function


even_no(lst).
print("Yuuvanesh S XII-D RollNo.22")
def even_no(l):
l1=[]
for i in l:
if i%2==0:
l1.append(i)
return l1
l=[]
n=int(input("Enter no. of element:"))
for i in range(n):
l.append(int(input("Enter element:")))
print(even_no(l))
3. Write function find(item,l) which finds item in
the list or not and return true if present else false.
print("Yuuvanesh S XII-D RollNo.22")
def find(item,l):
for i in l:
if i==item:
return True
else:
return False
l=[]
n=int(input("Enter no. of element:"))
for i in range(n):
l.append(int(input("Enter element:")))
print(find(int(input("Enter item:")),l))

4. Function to return the position of the item in the


provided list if item is found.
print("Yuuvanesh S XII-D RollNo.22")
def pos(item,l):
for i in range(len(l)):
if item==l[i]:
return i
else:
return -1
l=[]
n=int(input("Enter no. of element:"))
for i in range(n):
l.append(int(input("Enter element:")))
x=pos(int(input("Enter item:")),l)
if x>=0:
print("Position is",x+1)
else:
print("No item Found")
5. Write function to print list containing prime
numbers from the list passed by the user.
def prime(l):
l1=[]
for i in l:
for j in range(2,i//2):
if i%j==0:
break
else:
l1.append(i)
print(l1)
n=int(input("Enter no. of element:"))
l=[]
for i in range(n):
l.append(int(input("Enter element:")))
prime(l)

6. Function to display the perfect numbers present


in the passed list.
def perfect(l):
for i in l:
s=0
for j in range(1,i):
if i%j==0:
s=s+j
if s==i:
print(i,end=',')
l=[]
n=int(input("Enter no. of elements:"))
for i in range(n):
l.append(int(input("Enter element:")))
perfect(l)
7. Write function to find the armstrong number
present in the passed list.
print("Yuuvanesh S XII-D RollNo.22")
def arm(l):
for i in l:
a=i
s=0
while a>0:
r=a%10
s=s+r**len(str(i))
a=a//10
if s==i:
print(s,end=',')
l=[]
n=int(input("Enter no. of elements:"))
for i in range(n):
l.append(int(input("Enter element:")))
arm(l)

8. Write function to move the zeros present in the list


to the right.
print("Yuuvanesh S XII-D RollNo.22")
def zeroshift(l):
l1=[]
l2=[]
for i in l:
if i!=0:
l1.append(i)
else:
l2.append(i)
print(l1+l2)
l=[]
n=int(input("Enter no. of elements:"))
for i in range(n):
l.append(int(input("Enter element:")))
zeroshift(l)
9. Write function to shift the position of elements
in list towards rigtht to the given position.
print("Yuuvanesh S XII-D RollNo.22")
def position(l,pos):
l1=l[-pos:]+l[:-pos]
print(l1)
l=[]
n=int(input("Enter no. of element:"))
for i in range(n):
l.append(int(input("Enter element:")))
position(l,int(input("Enter no. of position:")))

10. Function to check whether a given string is a


pallindrome.
print("Yuuvanesh S XII-D RollNo.22")
def pallindrome(str):
l=str[::-1]
if str==l:
print("Pallindrome")
str=input("Enter string:")
pallindrome(str)
11.Function to find the number of words present in
the given line.
print("Yuuvanesh S XII-D RollNo.22")
def word(s):
print(len(s.split()))
s=input("Enter string:")
word(s)
12. Function to pass number of terms and print the
fibonacci series.
print("Yuuvanesh S XII-D RollNo.22")
def fibo(n):
a=-1
b=1
for i in range(n):
c=a+b
print(c,end=",")
a,b=b,c
fibo(int(input("Enter:")))
13.Function to find given no.’s factors or it is a prime no.
print("Yuuvanesh S XII-D RollNo.22")
def prime_factor(n):
a=True
for i in range(2,n//2):
if n%i==0:
break
else:
a=False
if a==True:
print("Factors are:")
for i in range(1,n+1):
if n%i==0:
print(i,end=",")
else:
print(n,"is a prime no.")
n=int(input("Enter no.:"))
prime_factor(n)
14. Write function to find HCF and LCM of two nos.
print("Yuuvanesh S XII-D RollNo.22")
def hcf_lcm(n,m):
if n>m:
s=m
else:
s=n
for i in range(1,s+1):
if n%i==0 and m%i==0:
hcf=i
print('hcf=',hcf,'lcm=',m*n/hcf)
n=int(input("enter n:"))
m=int(input("enter m:"))
hcf_lcm(n,m)

15. Function to check whether a given string is a


pallindrome.
print("Yuuvanesh S XII-D RollNo.22")
def arng_alpha(s):
a=sorted(s)
print("".join(a))
arng_alpha(input("enter:"))

17.Function takes a number n as argument, prints


random number having exactly n digits.
print("Yuuvanesh S XII-D RollNo.22")
import random
def ran(n):
a=random.randint(10**(n-1),(10**n)-1)
print(a)
n=int(input("enter:"))
ran(n)
16. Function to calculate the number of
alphabets,uppdercase,lowercase,digits,vowels, special
chraracter.
print("Yuuvanesh S XII-D RollNo.22")
def counter(s):
alpha=uc=lc=d=c=v=sc=0
for i in s:
if i.isalpha():
alpha+=1
if i.isupper():
uc+=1
if i.lower() not in "aeiou":
c+=1
elif i.lower() in "aeiou":
v+=1
elif i.islower():
lc+=1
if i.lower() not in "aeiou":
c+=1
elif i.lower() in "aeiou":
v+=1
elif i.isdigit():
d+=1
elif i not in " ":
sc+=1
print("Alphabet:",alpha)
print("Uppercase:",uc)
print("Lowercase:",lc)
print("Consonant:",c)
print("Vowel:",v)
print("Digit:",d)
print("Special Character:",sc)
s=input("Enter:")
counter(s)
18. Function to take two numbers and print number
having less value in its units place.
print("Yuuvanesh S XII-D RollNo.22")
def unit(a,b):
if str(a)[-1]>str(b)[-1]:
print(a)
else:
print(b)
a=int(input("Enter:"))
b=int(input("Enter:"))
unit(a,b)
19. Function to print a series by getting first and last
values frrom the user.
print("Yuuvanesh S XII-D RollNo.22")
def series(f,l):
print(f+1,end=",")
print(((l-f)//3)+f,end=",")
print((((l-f)//3)*2)+f,end=",")
print((((l-f)//3)*3)+f-1)
f=int(input("Enter f:"))
l=int(input("Enter l:"))
series(f,l)

20.Function accept gender and name to provide Mr.


and Mrs.
print("Yuuvanesh S XII-D RollNo.22")
def gender(gen,name):
if gen== "male" or "Male":
print("Mr.",name)
else:
print("Mrs.",name)
gender(input("Enter gender:"),
input("Enter name:"))
21. Function to print the roots of quadratic equations.
print("Yuuvanesh S XII-D RollNo.22")
import math
def quad(a,b,c):
d=((b**2)-(4*a*c))
if d>0:
s1=(-b+(math.sqrt(d)))/(2*a)
s2=(-b-(math.sqrt(d)))/(2*a)
print("Two real and unequal roots:",s1,s2)
elif d==0:
s1=-b/2*a
print("Two real and equal roots:",s1)
else:
print("Roots are imaginary")
a=int(input("Enter a:"))
b=int(input("Enter b:"))
c=int(input("Enter c:"))
quad(a,b,c)

23.Function to swap two nos. swapped if 1st>2nd one.


print("Yuuvanesh S XII-D RollNo.22")
def swap(a,b):
if a>b:
a,b=b,a
return a,b
else:
return a,b
print(swap(int(input("Enter:")),
int(input("Enter:"))))
22. Function assume n no. and prints random name.
print("Yuuvanesh S XII-D RollNo.22")
import random
def ran_name(l):
print(l[random.randint(0,len(l))])
l=[]
n=int(input("Enter no. of elements:"))
for i in range(n):
l.append(input("Enter element:"))
ran_name(l)

24.Function to calculate time taken for a object from a


floor of a building, user must input floor no.and height.
print("Yuuvanesh S XII-D RollNo.22")
def freq(s):
l=[]
for i in s:
l.append(i.lower())
d={}
for i in l:
d[i]=l.count(i)
print(d)
freq(input("Enter:"))
25. Function to find HCF and LCM- Menu Driven.
print("Yuuvanesh S XII-D RollNo.22")
def hcf(n,m):
if n>m:
s=m
else:
s=n
for i in range(1,s+1):
if n%i==0 and m%i==0:
hcf=i
return hcf
def lcm(n,m):
return m*n/hcf(n,m)
print("choose 1-HCF and2-LCM")
a=int(input("Enter choice:"))
if a==1:
print(hcf(int(input("enter
n:")),int(input("enter m:"))))
elif a==2:
print(lcm(int(input("enter
n:")),int(input("enter m:"))))
else:
print("Invalid choice")

26.Function accept string with mixed case then display


the frequency of characters irrespective of case.
print("Yuuvanesh S XII-D RollNo.22")
def freq(s):
l=[]
for i in s:
l.append(i.lower())
d={}
for i in l:
d[i]=l.count(i)
print(d)
freq(input("Enter:"))
27. Function to do Arithmetic Operation-Menu
Driven.
print("Yuuvanesh S XII-D RollNo.22")
def add(a,b):
print(a+b)
def sub(a,b):
print(a-b)
def mul(a,b):
print(a*b)
def div(a,b):
print(a/b)
a=int(input("enter:"))
b=int(input("enter:"))
s=int(input("enter 1 or 2 or 3 or 4:"))
if s==1:
add(a,b)
elif s==2:
sub(a,b)
elif s==3:
mul(a,b)
else:
div(a,b)

28.Temperature conversion using Menu Driven Function.


def f_c(t):
c=5/9*(t-32)
print("celsius is",c)
def c_f(t):
f=(9/5)*t+32
print("farenheit is",f)
print("1.farenheit to celsius")
print("1.celsius to farenheit")
s=int(input("enter 1 or 2:"))
if s==1:
t=float(input("Enter Fahrenheit"))
f_c(t)
elif s==2:
t=float(input("Enter Celsius"))
c_f(t)
29. Function to calculate area of any five shapes.
print("Yuuvanesh S XII-D RollNo.22")
def square(a):
print(a*a)
def rect(l,b):
print(l*b)
def circle(r):
print(3.14*r*r)
def triangle(b,h):
print((1/2)*b*h)
def cyl(r,h):
print(3.14*r*r*h)
print("1-square,2-rectangle,3-circle,4-
triangle,5-cylinder")
n=int(input("Enter choice;1 or 2 or 3 or 4 or
5:"))
if n==1:
square(int(input("Enter side of the
square:")))
elif n==2:
rect(int(input("Enter
length:")),int(input("Enter breadth:")))
elif n==3:
circle(int(input("Enter radius:")))
elif n==4:
triangle(int(input("Enter
base:")),int(input("Enter height:")))
elif n==5:
cyl(int(input("Enter
radius:")),int(input("Enter height:")))
else:
print("Invalid input")
30. Function to sort list in ascending order.
def asc(l):
print(sorted(l))
l=[]
n=int(input("Enter no. of elements:"))
for i in range(n):
l.append(int(input("Enter element:")))
asc(l)
31. Function to change elements first character in
the given list.
print("Yuuvanesh S XII-D RollNo.22")
def chr_chng(l):
l1=[]
for i in l:
if i[0].islower():
l1.append(i[0].upper()+i[1:])
else:
l1.append(i[0].lower()+i[1:])
print(l1)
len=int(input("How many?"))
l=[]
for i in range(len):
l.append(input("Enter:"))
chr_chng(l)

32. Function to convert Indian rupees to American dollar


and American dollar to Indian rupees
print("Yuuvanesh S XII-D RollNo.22")
def r_d(s):
if s==1:
d=r*82
print("rupees",d)
elif s==2:
d=r/82
print("dollar",d)
else:
print("wrong choice")
print("1.dollar-rupee,2.rupee-dollar")
s=int(input("enter choice 1 or 2:"))
if s==1:
r=int(input("enter dollar:"))
else:
r=int(input("enter rupees:"))
r_d(s)
33. Function to find cube of given number,else
calculate cube of 2 if argument not passed.
print("Yuuvanesh S XII-D RollNo.22")
def cube(n=2):
print(n**3)
cube(int(input("Enter:")))

34. Function to accept two arguments and checks both


are same.
print("Yuuvanesh S XII-D RollNo.22")
def same(a,b):
if a==b:
return 'same'
else:
return'not same'
print(same(input("Enter:"),input("Enter:")))

35. Function to accept two arguments and their lengths


are same.
print("Yuuvanesh S XII-D RollNo.22")
def same_len(a,b):
if len(a)==len(b):
return 'same'
else:
return'not same'
print(same_len(input("Enter:"),input("Enter:")))
36. Function to accept two argument x and n,prints nth
root of x.
print("Yuuvanesh S XII-D RollNo.22")
def nth_root(x=2,n=2):
print(x**(1/n))
x=int(input("Enter:"))
n=int(input("Enter:"))
nth_root(x,n)
37. Function to reverse a given
number
print("Yuuvanesh S XII-D RollNo.22")
def reverse(n):
s=0
while n>0:
r=n%10
s=s*10+r
n=n//10
print(s)
n=int(input("Enter no.:"))
reverse(n)

38. Get a dictionary in the form of serial number and


city. Display city name more than 5 digits.
print("Yuuvanesh S XII-D RollNo.22")
def city_name(d):
for i in d.values():
if len(i)>5:
print(i,end=",")
d={}
l=int(input("Enter the size:"))
for i in range(l):
k=input("Serial no.:")
v=input("City name:")
d[k]=v
city_name(d)

39. Function to check whether the given number is


divisible by 3 and 7.
print("Yuuvanesh S XII-D RollNo.22")
def div3_7(n):
if n%3==0 and n%7==0:
print(n,"Divisible by 3 and 7")
div3_7(int(input("Enter:")))
40.quiz with 5 questions and display the score.
print("Yuuvanesh S XII-D RollNo.22")
import random
def quiz():
d={"What is the capital of France?":
"Paris","Who wrote 'Romeo and Juliet'?": "William
Shakespeare","What is the largest planet in our
solar system?": "Jupiter","In which year did
World War II end?": "1945","What is the square
root of 64?": "8","Who painted the Mona Lisa?":
"Leonardo da Vinci","What is the currency of
Japan?": "Yen","Which element has the chemical
symbol 'H'?": "Hydrogen","Who is known as the
'Father of Computer Science'?": "Alan
Turing","What is the largest mammal on Earth?":
"Blue Whale"}
s=0
l=list(d.values())
l1=list(d.keys())
l2=[]
c=5
for i in range(c):
a=random.randint(0,9)
print(l1[a])
b=input("Give answer:")
if b.lower() in l[a].lower():
s+=1
print("Your score is:",s,"/",c)
quiz()

You might also like