0% found this document useful (0 votes)
11 views28 pages

Computer Science XII Practical Programs

Uploaded by

papag78609
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views28 pages

Computer Science XII Practical Programs

Uploaded by

papag78609
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Computer Science - XII Practical File Questions

Question 1: Write a program with a user defined function to create table of a


number.
Solution :
def table(n):
for i in range(1,11):
print(n, "*", i, "=", n*i)
n=int(input("Enter any number: "))
table(n)

Output :

Question 2: Write a program with a user defined function to create factorial


of a number.
Solution :
def factorial(n):
f=1
for i in range(1,n+1):
f=f*i
print("Factorial of number is: ", f)
n=int(input("Enter number for factorial: "))
factorial(n)

Output:

1
Question 3: Write a program with a user defined function to get reverse of
the number entered.
Solution :
def reverse(n):
r=0
while n>0:
d=n%10
r=r*10+d
n=n//10
print("Reverse number is: ", r)
n=int(input("Enter number to reverse it: "))
reverse(n)

Output :

Question 4: Write a program with a user defined function that multiplies


each even element of a list by 2 and each odd element by 3.
Solution:
def MULTI(A):
for x in range(len(A)):
if A[x]%2==0:
A[x]*=2
else:
A[x]*=3
print("List after change: ", A)
A=eval(input("Enter elements in list: "))
print("List before change: ", A)
MULTI(A)

Output :

2
Question 5: Write a program to find the largest and the smallest element in
a list using function.
Solution :
def list():
a=b=L[0]
for x in L:
if x>a:
a=x
if x<b:
x=b
print("Largest element is: ", a)
print("Smallest element is: ", b)
L=eval(input("Enter List Elements: "))
list()

Output :

Question 6: Write a program to search any element in a list and print index
of each occurrence of element in list using function.
Solution :
def List(A):
c=0
for i in range(len(A)):
if A[i]==x:
print("Element found at index: ", i)
c=1
if c==0:
print("Element is not in list.")
A=eval(input("Enter List Elements: "))
x=int(input("Enter element to be searched: "))
List(A)

Output :

3
Question 7: Write a program to create a dictionary which consists of
Record, Roll No., Name and Marks of students and display its content.
Solution :
L=[]
N=int(input("Enter number of records: "))
for i in range(N):
D={}
R=int(input("Enter Roll No.: "))
Nm=input("Enter Name: ")
m=int(input("Enter Marks: "))
D["Roll No."]=R
D["Name"]=Nm
D["Marks"]=m
[Link](D)
print("Details of Students: ")
for x in range(N):
print(L[x], end=" ")

Output :

4
Question 8: Write a program to create a text file and display its content.
Solution :
F=open("[Link]", "w")
ch="Y"
while ch=="Y":
s=input("Enter data to write on file: ")
[Link](s)
[Link]("\n")
ch=input("Enter Y/N for more data: ")
[Link]()
F=open("[Link]", "r")
x=[Link]()
print("Data from file: ", x)
[Link]()

Output :

Text File :

Question 9: Write a program to count the number of lines starting with the
letter “T” in a text file “[Link]”.
5
Solution :
def count():
F=open("[Link]","r")
c=0
x=[Link]()
for a in x:
if a[0]=="T":
c=c+1
print("Number of lines starting with the letter T: ", c)
[Link]()
count()

Output:

Text File :

Question 10: Write a program to count the number of lines starting with an
uppercase letter.
Solution :
def count():
F=open("[Link]","r")
c=0
x=[Link]()
for a in x:
if a[0].isupper():
c=c+1
print("Number of lines starting with an uppercase letter: ", c)
[Link]()
count()
Output:

Text File :

6
Question 11: WAP to create a text file line by line and display each word
separated by #

Solution:
F=open("[Link]", "w")
ch = "y"
while ch=='y':
s=input("Enter data to write on file")
[Link](s)
ch=input("Enter y to write more")
[Link]()
F=open("[Link]","r")
x=[Link]()
print("Data from file", x)
[Link]()

F=open("[Link]", "r")
x=[Link]()
for y in x:
words=[Link]()
for word in words:
print(word+"#", end=" ")
print(' ')

Output:

7
Question 12: Write a program to count the number of times the word
“Hello” is found in a text file “[Link]”.

Solution :
def count():
F=open("[Link]", "r")
c=0
s=[Link]()
x=[Link]()
for a in x:
if a=="Hello":
c=c+1
print("Number of times the word Hello is found: ", c)
[Link]()
count()

Output :

Text File :

Question 13: Write a program to read the content of a file "[Link]" and
count no. of words having length more than 5 character.

Solution:
f=open("[Link]","w")
ch = "y"
while ch=='y':
s=input("Enter data to write on file")
[Link](s)
[Link]("\n")
ch=input("Enter y to write more")
[Link]()

f=open("[Link]", "r")
c=0

8
x=[Link]()
y=[Link]()
for a in y:
if len(a)>5:
c=c+1
print("No of words having length more than 5 characters are", c )
[Link]()

Output:

Question 14: WAP to count no of time letter "T" is present in a text file in
"[Link]"

Solution:
f=open("[Link]","r")
c=0
a=[Link]()
for x in a:
if x=="T":
c=c+1
print ("No of times T id found ", c)
[Link]()

Output:

Text File :

9
Question 15: Write a program to create a binary file with Roll No., Name
and Marks of students in the form of a list. Read and display contents of the
file.(List).

Solution :
import pickle
def create():
F=open("[Link]", "wb")
ch="Y"
while ch=="Y":
L=[]
R=int(input("Enter Roll No.: "))
N=input("Enter Name: ")
M=int(input("Enter Marks: "))
L=[R,N,M]
[Link](L,F)
ch=input("Enter Y/N for more data: ")
[Link]()
def read():
F=open("[Link]", "rb")
try:
while True:
x=[Link](F)
print(x)
except EOFError:
[Link]()
create()
read()

Output :

10
Question 16: Write a User Defined function to create a binary file
"[Link]" by writing rollno, name and marks of students on it .
(Store data in the form of Dictionary). Write another function to read and
display contents of file.

Solution :
import pickle
def create():
f=open("[Link]","ab")
ch='y'
while ch=='y':
d={}
r=int(input("Enter Rollno"))
nm=input("Enter Name")
m=int(input("Enter Marks"))
d["rollno"]=r
d["name"]=nm
d["marks"]=m
[Link](d,f)
ch=input("Enter y or n to write more data")
[Link]()
def show():
f=open("[Link]","rb")
print ("Data from file")
try:
while True:
x=[Link](f)
print(x)
except EOFError:
[Link]()
create()
show()

Output :

11
Question 17: WAUDF to update Marks of a student whose RollNo is entered
by user in a binary file "[Link]" consist of data in the form of following
dictionary{"rollno":10,"name":"abc","marks":120}
Solution:
import pickle
def UPDATE():
f=open("[Link]","rb+")
r=int(input("Enter rollno to change marks"))
c=0
try:
while True:
pos=[Link]()
x=[Link](f)
if x["rollno"]==r:
print("Record of student",x)
x["marks"]=int(input("Enter New marks"))
[Link](pos)
[Link](x,f)
c=1
break
except EOFError:
if c==0:
print("Rollno Not found")
[Link]()
def show():
f=open("[Link]","rb")
print ("Updated Data from file")
try:
while True:
12
x=[Link](f)
print(x)
except EOFError:
[Link]()
UPDATE()
show()

Output:

Question 18: Write a program to create a binary file “[Link]” with P


No., Name and Price of products. Display details of products having price
more than 100.
Solution :
import pickle
def create():
F=open("[Link]", "wb")
ch="Y"
while ch=="Y":
D={}
P=int(input("Enter P No. : "))
Nm=input("Enter Name : ")
Pr=int(input("Enter Price : "))
D["Product No."]=P
D["Name"]=Nm
D["Price"]=Pr
[Link](D,F)
ch=input("Enter Y/N for more data : ")
[Link]()
def read():
F=open("[Link]", "rb")
try:
while True:
x=[Link](F)
if x["Price"]>100:
print(x)
except EOFError:
13
[Link]()
create()
read()

Output :

Question 19: Write a program to create a CSV file with Product Number,
Name and Price of product. Read and display contents of file.

Solution :
import csv
def create():
F=open("[Link]", "w")
ch='Y'
w=[Link](F)
[Link](["PNo","PName","Price"])
while ch=="Y":
PNo=int(input("Enter Product Number: "))
PName=input("Enter Name: ")
Price=int(input("Enter Price: "))
x=[PNo,PName,Price]
[Link](x)
ch=input("Enter Y/N to write more data: ")
[Link]()
def read():
F=open("[Link]","r",newline="\n")
r=[Link](F)
for x in r:
print(x)
[Link]()
create()
read()

14
Output :

Question 20: Write a user defined function to create a CSV file “[Link]”
with Train No., Train Name and Destination. Read and display contents of
the file.

Solution :
import csv
def create():
F=open("[Link]", "w")
ch='Y'
w=[Link](F)
[Link](["TNo","TName","Destination"])
while ch=="Y":
TNo=int(input("Enter Train Number: "))
TName=input("Enter Train Name: ")
Destination=input("Enter Destination: ")
x=[TNo,TName,Destination]
[Link](x)
ch=input("Enter Y/N to write more data: ")
[Link]()
def read():
F=open("[Link]","r",newline="\n")
r=[Link](F)
for x in r:
print(x)
[Link]()

15
create()
read()

Output :

Question 21: Write a program to implement stack.

Solution :
def push(s,x):
[Link](x)
def POP(s):
if s==[]:
print("Stack is empty")
else:
e=[Link]()
print("The deleted element is: ", e)
def peek(s):
if s==[]:
print("Stack is empty")
else:
top=len(s)-1
print("The element at top is: ", s[top])
def display(s):
if s==[]:
print("Stack is empty")
else:
top=len(s)-1
print("Stack elements are: ")
for i in range(top,-1,-1):
print(s[i],)
s=[]
ch1="Y"
16
while ch1=="Y":
print("Operations on linear stack")
print("Enter 1 : to push")
print("Enter 2 : to pop")
print("Enter 3 : to display")
print("Enter 4 : to peek")
ch=int(input("Enter your choice: "))
if ch==1:
x=int(input("Enter data to insert in stack: "))
push(s,x)
elif ch==2:
POP(s)
elif ch==3:
display(s)
elif ch==4:
peek(s)
else:
print("Wrong choice")
ch1=input("Enter Y to continue: ")

Output :

17
18
19
20
Question 22: Write a python program that displays first three rows fetched
from Student table of MySQL.

Solution :
import [Link] as sqltor
mycon=[Link](host="localhost", user="learner", passwd="fast", database="test")
if mycon.is_connected()==False:
print("Error connecting to MySQL database")
cursor=[Link]()
[Link]("SELECT * FROM Student")
data=[Link](3)
count=[Link]
for row in data:
print(row)
[Link]()

Output :
(101, ‘Ruhani’, Decimal(’76.80’), ‘A’, ‘A’, ‘Pending’)
(102, ‘George’, Decimal(’71.20’), ‘B’, ‘A’, ‘Submitted’)
(103, ‘Simran’, Decimal(’81.20’), ‘A’, ‘B’, ‘Evaluated’)

Table :

21
SQL PROGRAMS

22
Question 23: Consider the Personalities table given below:

Table: Personalities
Name Country Age Sex Field
Hilary Clinton U.S.A. 60 F Politics
Amitabh Bachachan India 65 M Acting
Michael Schumacher Germany 39 M Sports
Aishwarya Rai India 34 F Acting
L.N. Mittal U.K. 58 M Business
Roger Federer Switzerland 26 M Sports
Vijay Mallaya India 53 M Business
Shah Rukh Khan India 42 M Acting
Kiran Bedi India 59 F Civil Services
Abdul Kalam India 76 M Education

Query 1) Display the names of distinct countries from table personalities.


Command
SELECT DISTINCT Country
FROM Personalities;
Output

DISTINCT
Country
U.S.A.
India
Germany
U.K.
Switzerland

Query 2) List the maximum and minimum age from Personalities.


Command
SELECT MAX(Age), MIN(Age) FROM Personalities;
Output

MAX(Age) MIN(Age)
76 26

Query 3) Display number of distinct field available from table Personalities.


Command
SELECT COUNT (DISTINCT Field) FROM Personalities;
Output

COUNT (DISTINCT Field)

23
6

Query 4) Display name of all personalities who does not belong to India
from table Personalities.
Command
SELECT NAME FROM Personalities WHERE Country !=”INDIA”;
Output
Name
Hilary Clinton
Michael Schumacher
L.N. Mittal
Roger Federer

Query 5) Display name, age of all Female personalities from table


personalities.
Command
SELECT Name, Sex
FROM Personalities
WHERE Sex=”F”;
Output

NAME SEX
Hilary Clinton 60
Aishwarya Rai 34
Kiran Bedi 59

24
Question 24: Consider the Furniture table given below:

Table : FURNITURE
ITEM NAME TYPE DATEOFSTOCK PRICE DISCOUNT
White lotus Double bed 23/02/02 30000 25

Pink feather Baby cot 20/01/02 7000 20

Dolphin Baby Cot 19/02/02 9500 20

Decent Office Table 01/01/02 25000 30

Comfort zone Double Bed 12/01/02 25000 25

Donald Baby cot 24/02/02 6500 15

Royal Finish Office table 20/02/02 18000 30

Royal tiger Sofa 22/02/02 31000 30

Econo sitting Sofa 13/12/01 9500 25

Eating Paradise Dining Table 19/02/02 11500 25

Query 1) To show all information about the baby cots from the Furniture
table .
Command
SELECT *
FROM FURNITURE
WHERE TYPE = “Baby cot”;
Output

ITEM NAME TYPE DATEOFSTOCK PRICE DISCOUNT


Pink feather Baby cot 20/01/02 7000 20
Dolphin Baby Cot 19/02/02 9500 20
Donald Baby cot 24/02/02 6500 15

Query 2) To list the ITEM NAME which are priced at more than 15000 from
the FURNITURE Table.
Command
SELECT ITEM NAME
FROM FURNITURE
WHERE PRICE > 15000;

ITEM NAME
White lotus

25
Output Decent
Comfort zone
Royal Finish
Royal tiger
Eating Paradise

Query 3) To list the ITEM NAME and TYPE of those items, in which
DATEOFSTOCK in before 22/01/02 from FURNITURE in descending order
of ITEM NAME.
Command
SELECT ITEMNAME, TYPE
FROM FURNITURE
WHERE DATEOFSTOCK < {22/01/02} ORDER BY ITEMNAME desc;
Output

ITEM NAME TYPE


Pink feather Baby cot
Econo sitting Sofa
Decent Office Table
Comfort zone Double Bed

Query 4) To display ITEMNAME and DATEOFSTOCK of those items in


which the DISCOUNT percentage is more than 25 from FURNITURE
Table.
Command
SELECT ITEMNAME, DATEOFSTOCK
FROM FURNITURE WHERE DISCOUNT >25;
Output

ITEM NAME DATEOFSTOCK


Decent 01/01/02
Royal Finish 20/02/02
Royal Tiger 22/02/02

Query 5) To count the number of items, whose TYPE is “Sofa” from


FURNITURE Table.
Command
SELECT COUNT (*)
FROM FURNITURE
WHERE TYPE = “Sofa”;
Output

COUNT (*)
2
26
Question 25: Consider the Product and Client tables given below:

Table : PRODUCT
P_ID ProductName Manufacturer Price
TPO1 Talcom Powder LAK 40
FWO5 Face Wash ABC 45
BSO1 Bath Shop ABC 55
SHO6 Shampoo XYZ 120
FW12 Face Wash XYZ 95

Table : CLIENT
C_ID CilentName City P_ID
01 Cosmetic Shop Delhi FW05
06 Tatal Health Mumbai BS01
12 Live Life Delhi SH06
15 Pretty Woman Delhi FW12
16 Dreams Banglore TP01

Query 1) To display the details of those Clients whose City is Delhi.


Command
SELECT * FROM CLIENT
WHERE CITY = “Delhi”;
Output

C_ID CilentName City P_ID


01 Cosmetic Shop Delhi FW05
12 Live Life Delhi SH06
15 Pretty Woman Delhi FW12

Query 2) To display the details of Products whose price is in the range of 50


to 100 (Both values included)
Command
SELECT *
FROM PRODUCT WHERE PRICE BETWEEN 50 AND 100;
Output
P_ID ProductName Manufacturer Price
BSO1 Bath Shop ABC 55
FW12 Face Wash XYZ 95

27
Query 3) To display the Client Name, City from Table client , and Product
Name and Price from table Product, with their corresponding matching
P_ID.
Command
SELECT ClientName, City, ProductName, Price
FROM CLIENT, PRODUCT WHERE CLIENT.P_ID = PRODUCT.P_ID;
Output
CILENTNAME CITY PRODUCT NAME PRICE
Cosmetic Shop Delhi Face Wash 45
Tatal Health Mumbai Bath Shop 55
Live Life Delhi Shampoo 120
Pretty Woman Delhi Face Wash 95
Dreams Banglore Talcom Powder 40

Query 4) To display the MAXIMUM PRICE offered by each manufacturer:


Command
SELECT Manufacturer, MAX (Price)
FROM PRODUCT GROUP BY Manufacturer;
Output

Manufacturer MAX(Price)
LAK 40
ABC 55
XYZ 120

Query 5) Write a command to count No. of Manufacturers.


Command
SELECT COUNT (DISTINCT Manufacturer) FROM PRODUCT;
Output
COUNT (DISTINCT Manufacturer)
3

28

You might also like