CS Practical
CS Practical
CS Practical
the
three "PASS"PRINT
GRADE>502
inputted STOP 1S
numbers PRINT
"FAIL'
5.39
if i%2 0:
print (s)
WAPthat reads two numbers and an arithmetic operator and displays the results
Ans. Code:
number: '))
a float (input ('Enter first
number : ))
b float (input ('Enter second
Op input (" Enter the operator: ')
if Op=':
print (atb)
elif O p ;
print (a-b)
elif O p * :
print (a*b)
elif Op==/':
print (a/b)
elif Op==':
print (atb)
elif Op=**;
print (a**b)
else:
print ('Invalid option')
25. WAP toperform the following task according to the user's choice using menu.
(a) Area of Circle (b) Area of Rectangle
(c) Circumference of Circle (d) Area of Square
Ans. while True:
print (" 1. For Area of Circle")
print (" 2. For Area of Rectangle")
print (" 3. For Circumference of Circle")
print (" 4. For Area of Square")
ch=int (input ("Enter your choice (enter 0to exit): ")
if ch==l:
r=int (input ("Enter the radius of the Circle: "))
a=3.14*r*r
print ("Area of Circle: ",a)
elif ch==2:
l=int (input ("Enter the length:"))
b=int (input ("Enter the breadth: ") )
a=l*b
print ("Area of
elif ch==3: Rectangle: ",a)
r=int (input ("Enter the radius of
c=2* (int (3.14*r)) the Circle:"))
print
("Circumference of Circle: ", c)
5.40
Computer Science
]=int (input ("Enter the side of
a=l*1 square: "))
print ("Area of Square: ",a)
elif ch==0:
break
else:
print (" Invalid Option")
26.. Write a program to accept a character from the user and display whether it is avowel or consonant.
Ans. ch = input ("Enter a character: ")
if (ch = a ' or c h A ) :
print (ch, "is a vowel")
elif (ch e' or ch==E'):
print (ch, "is a vowel")
elif (ch ==i' or ch==I):
print (ch, "is a vowe l")
elif (ch =='o' or ch==O):
print (ch, "is a vowel")
elif (ch =='u! or ch=='U):
print (ch, "is a vowel")
else:
print (ch, "is a consonant")
number.
A7) WAP to accept anumber from the user and print the table of that
Ans. Code:
table is required: ") )
x= int (input ("Enter no for which
i = 1
while (i<=10) :
print (x, " x ",i, " = ",x*i)
it=1
to a certain limit.
28. WAP to print Fibonacciseries up
Ans. Code:
the limit:"))
i = int (input ("Enter
x =0
y = 1
z = 1
print ("Fibonacci series\n")
print (x, y,end= "")
while (z<=i) :
print (z, end=! )
y = Z
not.
z=X+y
number and checkiif it is a prime number or
29) Write a program to enter a
Ans. Code: number:'))
(input ('Enter the
1um=int
num//2+1) :
for i in range (2,
if num%i==0:
not a prime number')
print('It is
break
else: prime number')
print('It is a
5.41
Conditional I annd Looping Constructs
whether it is an Armstrong number
and display
:
out such that the or > 0
accept a number, find
digits is an integernumber thesum of the cubes< not. whilen
a=n%2
30.)WAP to Armstrongnumberofthree
371is an
Armstrong since 3**3 +
7**3 +1**3of its =371 m = m +(a
Note:An
tothe number
Ans. Code:
itself. For
("Enter
example,
3-digit
number: ")) diotsi e
print
(m)
C =C*10
n = i n t(
num=int (input
printth
fnum to
sum = 0 WAP
34.Code:
Ans.
while (f>0): X= int(inp
a = fl10 n= int(inp
f int (f/10)
total = 1
Sum = Sum+(at*3)
rar
") in
if (sum=num) : Armstrong number. for ti o t a l =
an
print (num, " is print(rOun
else: Armstrong number. ")
print(num, " is not an the tofind
th
accept a number and display
whether number is a Palindrome or not. N WAP
35/S= a+ar+ar'ta
(31. Write a program to Ans. Code:
Ans. Code: number: ")) (in
orig = int (input ("Enter a = int
nr= float (
num = orig
(ing
rev =0 a = int
while num > 0: print ("\n
digit = num 10 sum=0
rev = rev * 10 + digit for i in ra
num = int (num/10) term=a
if orig == rev: Sum=SU
print ('Palindrome') print("Sur
else:
36. WAP to print
print ('Not a Palindrome') 5 55 5
32 WAP to accept a number and find out whether it is a perfect number or not. 5 5 55
Note: Perfect number is a positive integer that is equal to the sum of its proper divisors. The smalestpet 5 5 5 5
number is 6. 5 5 5 5
Ans. Code: Ans. i = 3
i =1 while (i>
S = 0 j= 3
num = int (input ("Enter number:")) while
while i<num: pr
if num%i == 0:
j
s t= i print
i = i+1 i=i-l
if s== num: 37 WAP to prir
print ("It is a perfect no.") 1 2 3
else: 1 2
print ("It is not 1
a
perfect no. ")
33.)WAP to accept a decimal Ans. i =3
Ans. Code: number and display its binary number. while i>
n= int (input j =1
a =0 ("Enter no:") ) whil:
n = 0
C= 1
prin
i=i
5.42
Computer
withPt
Science
Gondit onal and
(a*c)
+
m= m
n= int (n/2)
print
(mj
print thesum of the series 1+x'/1l+x'/21+....*/(n)l-exponential series,
WAPto
34.
Ans.
Code:
value of x :"))
(input ("Enter the
int the l imit :"))
inLinput ("Enter
n-
total 1
1) :
range (1, n +
EOr Í in ((x**i) /i)
total total
(total, 2) )
print (IOund
the sum of the geometric series given below:
tofindtar'+ar+....arn
WAP
S= atartar
35
Ans.Code:
("Enter max value of the 0erie :"))
int (input ratio: "))
float (input ("Enter
(input ("Enter the first term: " ) )
int
Series; \n ")
print ("\n
sum=0
(1,n) :
i in range
for term-a* (r**i)
Sum-sum+term
series: ", sum)
Drint ("Sum of the
following pattern:
36.WAP to print the
555 5
555 5
555 5
555 5
Ans, i = 3
while (i>=0) :
j=3
while (j>=0) :
print (5, end= )
j- j-1
print()
ii-1
WAP to print the following pattern:
123
1
Ans, 3
while i>0:
while j<ui:
print (ý,end=! ')
print()
ii-1
5.43
Condtional and Looping Constructs
38.) WAP to print the
A
following pattern:
BC
DE F
GHIJ in
r
KL MNO for
ai fa B
1234 (iii) re
12345 (iv) n=
(i) 1 42. Maket!
3 3 for a
5 5 5
777 7 Ans. for a
999 99 pr
(iii) 43. Write
45 the m
3 4 5 Ans. n=i
2 3 4 5
max
1234 5
Ans. (i) n=5
for i in range (1, n+1) :
for jin range (1, i+1):
print (j, end=! ') pr
print ("")
(ii) n=10 44. W
for i in range or
(1,n+1,,2) :
for j in range
(0,i+1,2)
print (i, end=! ')
:
print ("")
(ii) for i in range
(5,0, -1) :
for j in range (i, 6) :
print (j, end=)
print () Ans.
40. Convert the following while loop to
a=3 for loop:
while a<i00:
if a%2==0:
else:
print (a+2, end=",")
a=a+3
print (a*2, sep="$")
break
else:
print ("String is a Palindrome ")
22. Write a program that reads aline, then counts words and displays how many words are
Ans. Code:
line=input ("Enter line: ")
there in the Iine.
x=line. split )
cnt=0
for i in x:
cnt=cnt+l
print ( cnt)
23 Write a program that reads a string and then prints a string that
e.g., school becomes sChOoL. capitalizes every other letter inthe
string,
Ans. Code:
string=input ("Enter a string: ")
length = len (string)
print ("original string:", string)
string2="n
for a in range (0, length) :
if a%2==0:
string2 += string [a]
else:
string2 += string [a] .upper (0
print (string2 )
6.32
with Pythor-
Computer Science
program that reads a line,
write a count. then counts how many
anddisplays the times a substring "is"
Ans.Coode:
appears in the ine
strl=input("Enter line: ")
str2="is"
J=strl. split ()
cnt=0
L:
for i in
if i==str2:
cnt+=1l
print ("Substring is appearing:",cnt)
25. Write a program that reads a string and displays the occurrence of words starting with a vowel in the
given string.
Example:
follows:
If the string content is as
Updated information issimplified by official websites.
The program should display the output as:
Updated
information
is
official
Vowel words: 4
Ans. cnt=0
strl= input ( "Enter a string: ")
word=strl. split ()
for i in word:
if i[0] in 'aeiou! or i[0]: in 'AEIOU':
cnt+=1
print (i)
print ("Vowe l wOrds : ",cnt) the given string.
a program that reads a string and displays the longest substring of
Z6) Write
Ans. Code:
Strl=input ("Enter a string: ")
Word=strl.split )
maxlength=0
maxword=""
for i in word:
X = len (i)
x>maxlength and i.isalpha () ==True :
1f
print (i)
maxlength=x
naxword=i is:",maXword)
length
print ("Substring with maximum
Output:
Enter a string: Hello Python
Hello
Python length is:
Python
Substring with maximum
6.33
24. Write a prOgram
which accepts a
1ST given as
under. If number the user and
is not in thefrom
LST = [3, 21, 5,number prints the
6, 3, 8, 21,LST,61 should print "Numberfrequency
it of the number in the list
Ans. LSTr = [3, 221, 5, 6, 3, not found."
8, 21, 61
length = len (LST)
element = int (input ("Enter element :"))
c= 0
for i in range (0, length) :
if element = LST ([il:
C=C+ 1
if c=0:
print ("Number not found")
else:
print (c)
Output:
Enter element: 21
2
laE Write a program in Python to find the maximum and minimum elements in the list
entered by the user.
Ans. lst = )
num = int (input('HoW many numbers : ))
for n in range (num) :
numbers = int (input ('Enter number:'))
lst.append (numbers)
print ("Maximum element in the list is :", max (lst))
print ("Minimum element in the list is :", min (lst))
Output:
How many numbers: 5
Enter number: 1
Enter number: 88
Enter number: 45
Enter number: 27 nguieYaciuoitseiavesebnggene
number:10E
Sf2
er? Enter
Maximun element in the list is : 88
Minimun element in the list is : 1
code:
2o. Write the output of the following Python program
STR1= list ("SNo4")
for i in range (len (STR1) ) :
if i==3 :
x=int (i)
s in x+=X-3
STR1 [i]=X
elif (sTR1 (i1.islower ()):
STR1 [i] = STR1 [i) .upper )
else:
STR1 [i)=sTR1 [i] *2
Print (STR1)
T'ss, 'NN, 0I '3']
7.51
L[i]=L[i]+10
else:
L[i]=L[i]+5
print (L)
Output:
[20, 30,,8, 110,
70, 92, 121
31.)WAPto display unique and
Input: L1- [2, 7,
1,4,9, duplicate items of a given list into two
output: (2, 7r l 5,1,4,31
4, 9, 5, 31 different ists.
[1, 4)
Ans. L1- (2,7, 1, 4, 9,5, 1, 4,3]
L2=[)
L3=[)
for i in Ll:
if i not in L2:
L2.append (i)
else:
L3.append (i)
print (L2)
print (L3)
Output:
(2, 7, 1, 4, 9, 5, 3]
[1, 4)
elements assuming list ishaing evem
32. WAP to exchange first-half elements of list with second-half
of elements.
Ans. L= [10, 20, 30, 40,50, 60,70]
x=int (len (L) /2)
for i in range (x) :
L[i],L[x+i] =L [x+i] ,L[i]
print (L)
Output:
140, 50, 60, 10, 20, 30, 70]
"sixty"l,6,7,8] and predi
33. Consider a nested list: 5, ["forty",
["computer", "device "] , 4,
Ll=(1,2, 3,
Outputs for the following statements: (i1) L1[3:6]
() L112] (iv) L1(3][0][0:4]
L1[3] [1]="Memory"
(ii) LI[3] [0] (vi) print (L1)
(v) L1[6] (1) (31
Ans. (0) 3 4, 51
(i) (['computer', "device'J,
(i)' computer'
(iv) 'comp 'sizty', 6, 7, 81
num [ i ], num
else:
i=i+2 (i+1]= num [ i+1], num[1)
i-i+l
print (num)
Output:
[3, 5, 21, 6, 8, 14, 3, 14)
39. weite a menu-driven
(a) Append program to perform various list
(c) Append a list to another list
operations:
(b) Insert
ial Delete existing (d) Modify element
Ans. listl= (]
element from given position (f) Delete existing elerment with
given value
neìnt (input ("How many
print ("Enter elements")
elements you want to enter: "))
for i in range (n):
ele=int (input () )
listl. append (ele)
nrint ("The entered list is: ",list1, '\n')
while True:
print ("\nLIST OPERATIONS")
print ("1. Append an element")
print ("2. Insert an element at desired positíon")
print ("3. Append a list to given list")
print ("4. Modify an existing element")
print ("5. Delete existing element by its position")
print ("6. Delete existing element by itsvalue")
print (""7. Exit\n")
choice=int (input ("Enter your choice (1-7) : "))
if choice==1:
element=int (input ("Enter the element to be appended: "))
list1.append (element)
print ( "New list: ", listl, '\n')
elif choice==2:
inserted: "))
element=int (input ("Enter the element to be
pos=int (input ("Enter the position: "))
list1. insert (pos, elemernt)
print ("New list: ",listl, '\n')
elif choice==3 :
to be appended: ")))
new_list=list (eval (input ("Enter the list
list1.extend (new list)
print ("New list: ",listl, '\n')
elif choice==4: "))
(input ("Enter position of element to be modified:
PoSint element: "))
new element=int ((input ("Enter new
Iist [pos] =new element
print ("New list: ", listl,'\n')
8.43
inaverage
store
and
and 32 existing
and than
result, more
not
if
is found
average
not
marks, display
if
"pass"
students
total and74 :"))
as above
result found
students,
(1-5)
all
of marks
assign if marks:"))
startswith
Key.
,'M')):
n
average
details choice
of average
namesand )
("Average:") i[2])
(('a','A','m'
(c) andother ("No
"))
average
menu:
marksgetting ("Total your
(Key,
]) input
key
displaystudents:
list") "-",
to
following
nested
marks,total students ")
("Name:
avg)
name=input Display")
("2.
print ("Enter
[ "February"]=29
Days program and (name)
append
names.
(input
(""1.
print Search")
("3.
print
Accept") [0],
marks,
(input Exit") tup:
Merit
name,name,
name of
":", menu-dri
the ven33 Display (n): )
"MENU" (input (i
print
==31:
Value
(b) " displaythanDisplay
Display Accept
marks=int in
range avg=float
(name,t
(t,
)tup+= ("4.
"5. if i
ch==1:
less List:(input Tuples
and
Dict
Days[ tupleResult:
print
[ if Displav:
Search: names=[] True:
while
(print ch=int
(print
print
for
"fail" Merit in
(a)
Day Ans. a n=nt
()tup= t=
(d) (e) Write (a) i
for
t
5. Ans.
8.44
Code:
Ans.
Aman
no:2222
Enter
EnterPhone
no:5555
Enter
Name: Enter
Pulkit
Enter
Name:
no:1111
ZiyaPhone Phone
Enter
no:9999
EnterEnter
Name:
AmanPhone
Name:Riya EnterEnter
Output: else:
26
for
X=input 1 while
i<=n:
phone=dict1i= () phone
i==X: if n=int
= Write
: i
phone. else: ch=5:
elif
phone name print
number print break
in = number a ch==4:
elif
b=input
phonei[a]=b
i+1 a=input (input Python
("Enter
to l:
(x, keys
no of (x,": ("Enter of
be program
("Enter
("Enter ("Invalid
choice")
aprint break ("MERIT
LIST")
print else:
is: entries: "does () particular
name. for
searched name
1111 phone ]>74:i[2if i
number to in
not to pos=names.index
(name1)
PhoneName: input print
[0])(i tup: print print
4 no be
Aman exist") 'n'
is:",searched: ") of ("Name (tup
no:") names
entries:")) [pos
phone and not
") phone [01,tupPlpos]
found")
[i])
kS\tAverage")PMat
numbers
to [1],
store
tup[pos]
omputer it
in
a
Science dictionary (211
with and
Pyth
Write a Python program to input names of n'
rent and conveyance allowance. Calculate totalemployees and their salary details like basic salary,
salary of each ermployee and nous*
Ans.Code: display.
dl=dict ()
i= 1
n=int (input ("Enter number of
entries:"))
while i<=n:
Nm=input ("\nEnter name of the employee;")
basicint (input ("Enter Basic salary:"))
=int (input ("Enter house rent allowance: "))
ca=int (input ("Enter conveyance allowance: "))
di(Nm] =(basic, hra,ca)
i = i+1
1= dl.keys ()
print (" \nName ",\t\t',"Net Salary") Note: list is stored in dictionary as value.
for i in l:
salary = 0
z= dl [il
for j in z:
salary-=salary+j
print (i, '\t\t',salary)
Output:
Enter number of entries: 4
Nishant
Enter name of the ernployee:
Enter Basic salary: 100000
30000
Enter house rent allowance:
5000
Enter conveyance allowance:
Enter name of the employee: Ram
Enter Basic salary: 50000
15000
Enter house rent allowance:
allowance: 5000
Enter Conveyance
Aushim
Enter name of the enp loyee:
Enter Basic salary : 200000
allowance: 50000
Enter house rent
allowance: 10000
iter Conveyance
Enter name of the emnployee: Riya
Enter Basic salary : 25000
2000
uiter house rent allowance:
1000
Enter convevance allowance:
Name Net Salary
Nishant 135000
Ram 70000
Aushim 260000
Riya 28000
their capital and lcurrency, store it in a dictionar
28. Wne aPython program to input names of 'n' countries andparticular country.
search and display for a
nd display intabular form. Also
Ans. Code:
d1=dict ()
i 1
of entries:"))
8.48
Ans.
while
True:
31
dict={}
for n=int () (e) (d) (c) dictionary
ch==4:
elif ch==3:
elif ch==2:
elif ch==1: ifExit") 7.
(b) (a) Write a
ch=int
(input 6. 5. 4. 3.print else: ch==4:
elif
[name]=ph
2. name=input
( dict(""Name:
no ph ") i DisplayCheck Add
ModifyDelete Display
namel=input dict.pop
(name1)
print
dict phname1=input print
namel=input DisplayCheck
Modify
Delete Add in
(input choice")
("Wrong
print
dict ph for no=int a
menu-driven
range else:
nol=int nol=int print(i, new
"MENU\nl. the ifathe anew the as
particular key-value
[name1] if
breakfound")
print
("Not
("Modified ("Modified[name ini dictionary
("No friendphonekey-value name
an a (input (n):
dict: dictionary a name name
1]=ph ("Enter existing name of is to
(input (input present
number friend and program
pair.
=ph ("Name ("Name ("Enter \n\ \n\
"-", Display ("Phone friends: in pairphone
is sorted Perform
dictionary: nol dict from in
your there\n\
nol("New dictionary:",("Enter in of this
of of number\n\
in the an numbers input
name: order the
friend: friend name number: ")) existing
dictionary dictionary the
ph
[i] choice sorted
dictionary. your
) of following
no:")) phone ") & offriends'
Phone names. friend. all
", to order\n\
(1-6) "))
") or and your
dict) bedict)number: not. operationsnames
display
numbers\n\ friends.
deleted: :")
) the and
") on
their
modified
) the
") phone
dictionary:
dictionary.
er numbers
and
ence store
them
with
in
the
Python-X