Class 12 Practical File 23-24
Class 12 Practical File 23-24
COMPUTER SCIENCE
CODE:083
ACADEMIC YEAR: 2023-24
ROLL NO :
NAME :
CLASS : XII
PGT (CS)
1
VARDAAN INTERNATIONAL ACADEMY
CERTIFICATE
Roll No: __________ has successfully completed the practical work in Computer
Science (083) under guidance of Mr. Syed Zishan Abbas during the academic year
2023-24
2
INDEX
Page
S. No. Name of the Practical Date Signature
No.
Input any number from user and calculate factorial of
01 06-04-23 05
a number.
Input any number from user and check, it is prime
02 number or not. 12-04-23 05-06
3
18 Write a python program to check whether a string is a 03-08-23 21-22
palindrome or not using stack.
4
Program 1: Input any number from user and calculate factorial of a number.
fact=1
n=num
while num>1:
fact=fact*num
num=num-1
OUTPUT:
Program 2: Input any number from user and check it is Prime number or not.
import math
isPrime=True
if num%i==0:
isPrime=False
if isPrime:
print("*Number is Prime*")
else:
5
OUTPUT:
def findSum(lst,num):
if num==0:
return 0
else:
return lst[num-1]+findSum(lst,num-1)
mylist=[] #Empty List
# Loop toinput in list
num=int(input("Enter how many number :"))
for i in range(num):
n=int(input("Enter Element"+str(i+1)+":"))
mylist.append(n) #Adding number to list
sum=findSum(mylist,len(mylist))
print("Sum of the List items",mylist,"is :",sum)
OUTPUT:
6
Program 4: Write a program to print Fibonacci series up to nth term.
a=0
b=1
sum = 0
count = 1
count += 1
a=b
b = sum
sum = a + b
OUTPUT:
7
Program 5: WAP to accept a string and display whether it is a Palindrome or not.
l=len(str)
p=l-1
index=0
while (index<=p):
if(str[index]==str[p]):
index=index+1
p=p-1
else:
break
else:
OUTPUT:
8
Program 6: WAP for calculating simple interest.
simple_interest = (principal*time*rate)/100
OUTPUT:
9
Program 7: WAP to accept percentage of a student and display its grade accordingly.
avg=((a+b+c+d+e)/500)*100
if(avg>=80):
print ("Congratulations! You have secured first Class and your % is ", avg)
print ("Congratulations! You have secured Second Class and your % is ", avg )
print ("Congratulations! You have secured Third Class and your % is ", avg)
elif (avg<40):
OUTPUT:
10
Program 8: WAP to accept a number and find out whether it is a perfect number or not.
#Program to accept a number and find out whether it is a perfect number or not.
i=1
s=0
num=int (input ("Enter number:"))
while i<num:
if num%i==0:
s=s+i
i=i+1
if s==num:
print ("It is a perfect no.")
else:
print ("It is not a perfect no.")
OUTPUT:
Program 9: Read a text file line by line and display each word separated by a #.
#Program to read content of file line by line and display each word separated by '#'
f=open("file1.txt")
for line in f:
words=line.split()
for w in words:
print(w+'#',end='')
print()
f.close()
OUTPUT:
11
Program10: Read a text file and display the number of vowels/consonants/
uppercase/lowercase characters in the file.
OUTPUT:
12
Program 11: WAP to remove all the lines that contain the character 'a' in a file
and write it to another file.
f=open('file1.txt','r')
lines=f.readlines()
f.close()
f=open('file1.txt','w')
f1=open("file2.txt","w")
print("All lines that contains a character has been removed from file1.txt")
print("All lines that contains a character has been saved in file2.txt")
f.close()
f1.close()
OUTPUT:
13
Program 12: Create a binary file with name and roll number. Search for a given
roll number and display the name, if not found display appropriate message.
import pickle
stud_data={}
list_of_students=[]
no_of_students=int(input("Enter no.of students:"))
for i in range(no_of_students):
stud_data["roll_no"]=int(input("Enter roll no.:"))
stud_data["name"]=input("Enter name:")
list_of_students.append(stud_data)
stud_data={}
file=open("stud_data.dat","wb")
pickle.dump(list_of_students,file)
file.close()
def search():
file=open("stud_data.dat","rb")
list_of_students=pickle.load(file)
roll_no=int(input("Enter roll no. to search:"))
found=False
for stud_data in list_of_students:
if(stud_data['roll_no']==roll_no):
found=True
print(stud_data['name'],"name, Found in file.")
if(found==False):
print("No student data found. please try again")
search()
file.close()
OUTPUT:
14
Program 13: Create a binary file with roll number, name and marks. Input a roll number
and update the marks.
import pickle
student=[]
f=open("student.dat","wb")
ans='y'
while ans.lower()=='y':
roll=int(input("Enter Roll Number:"))
name=input("Enter Name:")
marks=int(input("Enter Marks:"))
student.append([roll,name,marks])
ans=input("Add More ?(Y)")
pickle.dump(student,f)
f.close()
f=open("student.dat","rb+")
student=[]
while True:
try:
student=pickle.load(f)
except EOFError:
break
ans='y'
while ans.lower()=='y':
found=False
r=int(input("Enter roll number to update:"))
for s in student:
if s[0]==r:
print("Name is:",s[1])
print("Current Marks is:",s[2])
m=int(input("Enter new Marks:"))
s[2]=m
print("Record Updated!!!")
found=True
break
if not found:
print("Sorry! Roll Number not found!!!")
ans=input("Update more?(Y):")
f.close()
OUTPUT:
15
Program14: Write a random number generator that generates random numbers
between 1 and 6 (simulates a dice).
import random
while True:
print("="*50)
print(" *****ROLLING THE DICE *****")
print("="*50)
num=random.randint(1,6)
if num==6:
print("Hey...You got ",num,"Congrats!!!")
elif num==1:
print("Well tried but you got",num)
else:
print("You got:",num)
ch=input("Roll Again?(Y/N)")
if ch in 'Nn':
break
print("Thanks for playing!!!!")
OUTPUT:
16
Program15: Create a CSV file by entering user-id and password, read and search the
password for given user id.
# CSV file by entering user-id and password, write, read and search
import csv
def write():
f=open("details.csv","w",newline='')
wo=csv.writer(f)
wo.writerow(["User Name","Password"])
while True:
u_id=input("Enter User ID:")
pswd=input("Enter Password:")
data=[u_id,pswd]
wo.writerow(data)
ch=input("Do you wanr to enter more record(Y/N)")
if ch in 'Nn':
break
f.close()
def read():
f=open("details.csv","r")
ro=csv.reader(f)
next(ro) #for ignoring heading of records
for i in ro:
print(i)
f.close()
def search():
f=open("details.csv","r")
found=0
u=input("Enter User ID to be search:")
ro=csv.reader(f)
next(ro)
for i in ro:
if i[0]==u:
print("Password is:",i[1])
found=1
if found==0:
print("Sorry...No Record Found!!!!")
f.close()
write()
read()
search()
17
OUTPUT:
Program 16: Write a menu driven program to add, delete and display the record of
Students, hostel using list as stack data structure in python.
Record of hostel contains the fields: Hostel number, Total Students and Total Rooms.
host=[ ]
ch='y'
def push(host):
hn=int(input("Enter hostel number:"))
ts=int(input("Enter Total students:"))
tr=int(input("Enter total rooms:"))
temp=[hn,ts,tr]
host.append(temp)
def pop(host):
if(host==[]):
print("No Record")
else:
val=host.pop()
print("Deleted Record is :",val)
def display(host):
l=len(host)
print("Hostel Number\tTotal Students\tTotal Rooms")
for i in range(l-1,-1,-1):
print(host[i][0],"\t\t",host[i][1],"\t\t",host[i][2])
while(ch=='y' or ch=='Y'):
print("1. Add Record\n")
print("2. Delete Record\n")
print("3. Display Record\n")
18
print("4. Exit")
if(op==1):
push(host)
elif(op==2):
pop(host)
elif(op==3):
display(host)
elif(op==4):
break
OUTPUT:
19
Program 17: Write a program to implement a stack for the employee details (empno,
name,sal).
employee=[]
def push():
empno=input("Enter empno ")
name=input("Enter name ")
sal=input("Enter sal ")
emp=(empno,name,sal)
employee.append(emp)
def pop():
if(employee==[]):
print("Underflow / Employee Stack in empty")
else:
empno,name,sal=employee.pop()
print("poped element is ")
print("empno ",empno," name ",name," salary ",sal)
def traverse():
if not (employee==[]):
n=len(employee)
for i in range(n-1,-1,-1):
print(employee[i])
else:
print("Empty , No employee to display")
while True:
print("1. Push")
print("2. Pop")
print("3. Traversal")
print("4. Exit")
ch=int(input("Enter your choice "))
if(ch==1):
push()
elif(ch==2):
pop()
elif(ch==3):
traverse()
elif(ch==4):
print("End")
break
else:
print("Invalid choice")
20
OUTPUT:
Program 18: Write a python program to check whether a string is a palindrome or not using
stack.
class Stack:
def __init__(self):
self.items = []
def is_empty(self):
return self.items == []
def pop(self):
return self.items.pop()
s = Stack()
text = input('Please enter the string: ')
reversed_text = ''
while not s.is_empty():
reversed_text = reversed_text + s.pop()
if text == reversed_text:
print('The string is a palindrome.')
else:
21
OUTPUT:
Program 19: Write a menu driven program using function Qadd( ), Qdel( ) and disp( ) to
add, delete and display the record of book using queue as data structure in python.
book=[ ]
ch='y'
def Qadd(book):
bn=input("Enter book name:")
bnum=int(input("Enter book number:"))
bp=int(input("Enter book price:"))
temp=[bn,bnum,bp]
book.append(temp)
def Qdel(book):
if(book==[ ]):
print("No Record!!!")
else:
print("Deleted Record is :",book.pop(0))
def disp(book):
l=len(book)
print("Book Name\tBook Number\tBook Price:")
for i in range(0,l):
print(book[i][0],"\t\t",book[i][1],"\t\t",book[i][2])
while(ch=='y' or ch=='Y'):
print("1. Add Record\n")
print("2. Delete Record\n")
print("3. Display Record\n")
print("4. Exit")
op=int(input("Enter the Choice:"))
if(op==1):
Qadd(book)
elif(op==2):
Qdel(book)
elif(op==3):
disp(book)
elif(op==4):
break
ch=input("Do you want to enter more(Y/N):")
22
OUTPUT:
Consider the following MOVIE table and write the SQL queries based on it.
23
24
21. Queries Set 2 (Based on Functions):
25
26
22) Queries Set 3 (DDL Commands)
27
23) Queries set 4 (Based on Two Tables)
28
29
24) Queries Set 5 (Group by , Order By):
30
31
32
33
34
35
36
37