Grade XII - Computer Science Practical Manual
Grade XII - Computer Science Practical Manual
PRACTICAL
MANUAL
CLASS: XII
1
INDEX
2 FIBONACCI SERIES 4
3 STRING PALINDROME 5
4 RANDOM NUMBER 6
5 LIST ROTATION 7
6 FREQUENCIES OF ELEMENT 8
7 PRIME NUMBER 9
8 SIMPLE INTEREST 10
2
1. FACTORIAL OF A NUMBER
AIM:
PROGRAM:
fact = 1
fact = fact*i
return fact
OUTPUT
RESULT:
The program was successfully executed and desired output was obtained.
3
2. FIBONACCI SERIES
AIM:
PROGRAM:
a=0
b=1
sum = 0
count = 1
while (count<= n ) :
count += 1
a=b
b = sum
sum = a +b
OUTPUT
Fibonacci series: 0 1 1 2 3 5
RESULT:
The program was successfully executed and desired output was obtained.
4
3. STRING PALINDROME
AIM:
PROGRAM:
return False
return True
if (ans) :
else :
OUTPUT
RESULT:
The program was successfully executed and desired output was obtained.
5
4. RANDOM NUMBER GENERATOR
AIM:
PROGRAM:
import random
def rolladice () :
r = random.randint (1,6)
return r
n = ‘y’
n = input (‘Enter y/n to roll a dice and get a random number :’)
if n == 'y' :
print (rolladice())
OUTPUT
Rolling a dice
RESULT:
The program was successfully executed and desired output was obtained.
6
5. LIST ROTATION
AIM:
PROGRAM:
testlist = [1,4,6,7,2,]
OUTPUT
RESULT:
The program was successfully executed and desired output was obtained.
7
6. FREQUENCIES OF ELEMENTS
AIM:
PROGRAM:
L = [3,21,5,6,3,8,21,6]
L1= []
L2=[]
for i in L :
if i not in L2 :
x = L.count(i)
L1.append(x)
L2.append(i)
OUTPUT
Element Frequency
3 2
21 2
5 1
6 2
8 1
RESULT:
The program was successfully executed and desired output was obtained.
8
7. PRIME NUMBER
AIM:
PROGRAM:
import math
isprime = True
if num %i == 0:
isprime=False
break
if isprime :
else :
OUTPUT
RESULT:
The program was successfully executed and desired output was obtained.
9
8. SIMPLE INTEREST
AIM:
PROGRAM:
def simpleinterest(p,n ) :
if p> 30000:
return p*n*7/100
return p*n*5/100
else :
return p*n*3/100
OUTPUT
RESULT:
The program was successfully executed and desired output was obtained.
10
9. TEXT FILE – READ AND DISPLAY WORD WITH #
AIM:
To write a program to read text file line by line and display each word separated by #.
myfile.txt content
PROGRAM:
contents = fin.readlines()
words = line.split ()
for i in words :
fin.close()
OUTPUT
RESULT:
The program was successfully executed and desired output was obtained.
11
10. TEXT FILE - COPY LINES
AIM:
To write a program to copy all the lines containing the character ‘a’ or ‘A’ from a
file to another file.
file1.txt Contents
PROGRAM:
contents = fin.readlines ()
for i in line :
if i in['a' ,'A']:
fout.write(line)
break
fin.close()
fout.close()
RESULT:
The program was successfully executed and desired output was obtained.
12
11. TEXT FILE - COUNT CHARACTERS
AIM:
science.txt contents
PROGRAM:
def cnt () :
fin=open (‘science.txt',’r’)
contents = fin.read()
vowels=0
cons=0
lower=0
upper=0
for ch in contents :
if ch.islower():
lower +=1
elif ch.isupper () :
upper +=1
ch = ch.lower ()
vowels +=1
elif ch in [‘b’,’c’,’d’,’f’,’g’,’h’,’j’,’k’,’l’,’m’,’n’,’p’,’q’,’r’,’s’,’t’,’v’,’w’,’x’,’y’,’z’ ]:
cons +=1
fin.close ()
13
print (‘No of upper case letters are’ , upper)
cnt ()
OUTPUT:
No of vowels are 36
No of consonants are 48
RESULT:
The program was successfully executed and desired output was obtained.
14
12. CREATE A CSV FILE
AIM:
PROGRAM:
import csv
writer = csv.writer(fout)
while True :
userid = input(‘Enter id ‘ )
fout.writerow(record)
if x not in ‘yY’ :
break
reader = csv.reader(fin)
for i in reader :
next(reader)
if i [0]==uid:
print(i[1])
break
OUTPUT
Enter id 1
15
Enter id 2
abc
RESULT:
The program was successfully executed and desired output was obtained.
16
13. BINARY FILE - CREATE AND SEARCH RECORDS
AIM:
PROGRAM:
import pickle
stud={}
ans = 'y'
stud[‘Rollno’] = rno
stud[‘Name’] = name
stud[‘Mark1’] = mark1
stud[‘Mark2’]=mark2
stud[‘Mark3’]=mark3
pickle.dump(stud,fout)
fout.close()
stud1 = {}
found = False
try :
while True:
17
stud1 = pickle.load(fin)
if stud1[‘Rollno’] == r :
print (stud1)
found = True
except :
if found== False :
else:
fin.close()
OUTPUT
Students details
{'Rollno': 101, 'Name': 'Arun', 'Mark1': 56, 'Mark2': 65, 'Mark3': 76}
Record found
RESULT:
The program was successfully executed and desired output was obtained.
18
14. BINARY FILE - CREATE AND UPDATE RECORDS
AIM:
PROGRAM:
import pickle
stud={}
ans = 'y'
stud[‘Rollno’] = rno
stud[‘Name’] = name
stud[‘Mark1’] = mark1
stud[‘Mark2’]=mark2
stud[‘Mark3’]=mark3
pickle.dump(stud,fout)
fout.close()
stud1 = {}
found = False
try :
while True:
19
rpos = fin.tell()
stud1 = pickle.load(fin)
if stud1[‘Rollno’] == r:
stud1[‘Mark1’] += 5
fin.seek (rpos)
print (stud1)
found = True
except :
if found== False :
else:
fin.close()
OUTPUT
Students details
20
{'Rollno': 101, 'Name': 'Arun', 'Mark1': 61, 'Mark2': 65, 'Mark3': 76}
RESULT:
The program was successfully executed and desired output was obtained.
21
15. STACK – PUSH AND POP OPERATION
AIM:
To write a program to implement stack PUSH() and POP() operation using list.
PROGRAM:
travel = []
travel.append([row[0], row[1]])
def popelement () :
print (travel.pop())
else :
nlist = [[‘New York ‘ , ‘US’ , 11734 ] , [‘Naypyidaw’ , ‘Myanmar’ , 5219 ], [‘Dubai’ , ‘UAE’ ,
2194 ], [‘London’ , ‘England’ , 6693], [‘Colombo’ , ‘Srilanka’ , 3405 ]]
pushelement(nlist)
popelement()
OUTPUT
['Colombo', 'Srilanka']
['Dubai', 'UAE']
Stack empty
RESULT:
The program was successfully executed and desired output was obtained.
22
16. SQL - COMPANY AND CUSTOMER TABLE
AIM:
To create table company and customer. Write and execute queries for the question 1
to 5.
TABLE: COMPANY
TABLE: CUSTOMER
1. To display those company name which are having price less than 30000
OUTPUT
NAME
ONIDA
SONY
OUTPUT
NAME
SONY
ONIDA
23
NOKIA
DELL
BLACK BERRY
3. To increase the price by 1000 for those customers whose name starts with ‘S’
UPDATE CUSTOER SET PRICE = PRICE + 1000 WHERE NAME LIKE ‘S%’;
OUTPUT
4. To add one more column TOTALPRICE with float type to the table customer.
OUTPUT
OUTPUT
RESULT:
The queries were successfully executed and desired output was obtained.
24
17. SQL – TEACHER TABLE
AIM:
To create table teacher. Write and execute queries for the question 1 to 5.
TABLE: TEACHER
OUTPUT
OUTPUT
DISTINCT DEPARTMENT
COMPUTER
MATHS
OUTPUT
OUTPUT
25
COUNT(*)
3
OUTPUT
DEPARTMENT COUNT(*)
COMPUTER 2
MATHS 3
RESULT:
The queries were successfully executed and desired output was obtained.
26
18.PYTHON APPLICATION WITH MYSQL – FETCH RECORDS
AIM:
PROGRAM:
if mycon.is_connected()==False :
cursor=mycon.cursor()
data = cursor.fetchall()
print(row)
mycon.close()
OUTPUT
RESULT
The program was successfully executed and desired output was obtained.
27
19.PYTHON APPLICATION WITH MYSQL – COUNT RECORDS
AIM:
PROGRAM:
if mycon.is_connected()==False :
cursor=mycon.cursor()
data = cursor.fetchone()
count=cursor.rowcount
data = cursor.fetchone()
count=cursor.rowcount
data = cursor.fetchmany(3)
count=cursor.rowcount
mycon.close()
OUTPUT
RESULT
The program was successfully executed and desired output was obtained.
28
20.PYTHON APPLICATION WITH MYSQL - SEARCH RECORD
AIM:
PROGRAM:
if mycon.is_connected()==False :
cursor=mycon.cursor()
data = cursor.fetchone()
print(data)
mycon.close()
OUTPUT
RESULT
The program was successfully executed and desired output was obtained.
29
21.PYTHON APPLICATION WITH MYSQL - DELETE RECORD
AIM:
PROGRAM:
if mycon.is_connected()==False :
cursor=mycon.cursor()
print(‘Record Deleted’)
mycon.commit()
mycon.close()
OUTPUT
Record Delete
RESULT
The program was successfully executed and desired output was obtained.
30
22. SQL – TABLE STUDENT
AIM:
To create table student. Write and execute queries for the question 1 to 5.
TABLE: STUDENT
OUTPUT
OUTPUT
OUTPUT
31
4 RUBINA 350.00 SCIENCE 64.4 C 12B
OUTPUT
RESULT:
The queries were successfully executed and desired output was obtained.
32
23. SQL – TABLE DOCTOR AND SALARY
AIM:
To create table doctor and salary. Write and execute queries for the question 1 to 5.
TABLE: DOCTOR
1. Display name of all doctors who are in MEDICINE department having more than
10 years of experience.
SELECT NAME FROM DOCTOR WHERE DEPT =’MEDICINE’ AND EXPERIENCE > 10;
OUTPUT
NAME
BILL
2. Display the average salary of all doctors working in ‘ ENT’ department. Where
salary = Basic + Allowance.
OUTPUT
AVG(BASIC+ALLOWANCE)
33
13000.00
OUTPUT
MIN(ALLOWANCE)
1700.00
4. Display Id, Name from the Doctor table and Basic, Allowance From Salary Table
with their matching Id.
OUTPUT
OUTPUT
DEPT
ENT
ORTHOPEDIC
CARDIOLOGY
SKIN
MEDICINE
RESULT:
The queries were successfully executed and desired output was obtained.
34
24. SQL - AGGREGATE FUNCTIONS
AIM:
To create table marks. Write and execute queries for the question 1 to 5.
TABLE: MARKS
OUTPUT
SUM(COMPUTER)
409
OUTPUT
AVG(COMPUTER)
81.80
OUTPUT
COUNT(*)
5
OUTPUT
MIN(BIOLOGY)
45
35
5. To display maximum mark in chemistry
OUTPUT
MAX(CHEMISTRY)
83
RESULT:
The queries were successfully executed and desired output was obtained.
36
ANNEXURE
CREATING DATABASE
ACCESS DATABASE
USE SCHOOLDB;
TABLE: COMPANY
TABLE CREATION:
CREATE TABLE COMPANY (CID INT PRIMARY KEY, NAME VARCHAR(25), CITY
VARCHAR(25), PRODUCTNAME VARCHAR(25));
ADDING ROWS:
TABLE: CUSTOMER
TABLE CREATION:
CREATE TABLE CUSTOMER (CUSTID INT PRIMARY KEY, NAME VARCHAR(25), PRICE
FLOAT, QTY INT, CID INT);
37
ADDING ROWS:
EXP 17
TABLE: TEACHER
CREATE TABLE TEACHER (NO INT PRIMARY KEY, NAME VARCHAR(25), AGE INT,
DEPARTMENT VARCHAR(25), DATEOFJOIN DATE, SALARY INT, SEX VARCHAR(1));
ADDING ROWS:
EXP 22
TABLE: STUDENT
TABLE CREATION:
CREATE TABLE STUDENT (NO INT PRIMARY KEY, NAME VARCHAR(25), STIPEND
FLOAT, STREAM VARCHAR(25), AVGMARK FLOAT, GRADE VARCHAR(1), CLASS
VARCHAR(5));
ADDING ROWS:
TABLE CREATION:
CREATE TABLE DOCTOR (ID INT PRIMARY KEY, NAME VARCHAR(25), DEPT
VARCHAR(25), SEX VARCHAR(1), EXPERIENCE INT);
ADDING ROWS:
TABLE: SALARY
TABLE CREATION:
CREATE TABLE SALARY (ID INT PRIMARY KEY, BASIC INT, ALLOWANCE INT,
CONSULTATION INT );
ADDING ROWS:
39
EXP 24
TABLE: MARKS
TABLE CREATION:
CREATE TABLE MARKS (SID INT PRIMARY KEY,COMPUTER INT, PHYSICS INT,
CHEMISTY INT, BIOLOGY INT);
ADDING ROWS:
40