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

Practical Programs Class Xii

The document provides details about a Computer Science practical file from Class 12 of Kendriya Vidyalaya No.-2 located in Surat, Gujarat. It contains 7 programming problems and their solutions related to topics like searching elements in a list, calculating digit sum, finding largest element, counting characters in a string, displaying prime numbers, reading and processing text files.
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)
73 views28 pages

Practical Programs Class Xii

The document provides details about a Computer Science practical file from Class 12 of Kendriya Vidyalaya No.-2 located in Surat, Gujarat. It contains 7 programming problems and their solutions related to topics like searching elements in a list, calculating digit sum, finding largest element, counting characters in a string, displaying prime numbers, reading and processing text files.
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
You are on page 1/ 28

KENDRIYA VIDYALAYA NO.

-2, KRIBHCO SURAT


HAZIRA ROAD, SURAT-394515
STATE-GUJARAT
AHMEDABAD REGION

CLASS-XII-2021
SUBJECT- COMPUTER SCIENCE [083]

PRACTICAL FILE
# 1. Write a function to search an element in a list using Binary search
method.
def binarysearch(l2,n2,item2):
ans=0
beg=0
end=n2-1
while(beg<=end):
mid=int((beg+end)/2)
if(l2[mid]==item2):
ans=1
break
if(l2[mid]<item2):
end=mid-1
if(l2[mid]>item2):
beg=mid+1
return(ans)
# main program
l1=[]
n1=int(input("Enter number of elements you want to store:"))
for i in range(n1):
a=int(input("Enter element:"))
l1.append(a)
item1=int(input("Enter any item you want to search:"))
ans1=binarysearch(l1,n1,item1)
if(ans1==0):
print("Search is not success")
else:

print("Search is success")

OUTPUT OF PROGRAM-1
Enter number of elements you want to store:10

Enter element:1

Enter element:2

Enter element:3

Enter element:4

Enter element:5

Enter element:6

Enter element:7

Enter element:8

Enter element:9

Enter element:10

Enter any item you want to search:5

Search is success
#2. Write a function to calculate the sum of all digits of an entered number.
def calsum(n1):

sum1=0

while(n1>0):

rem=n1%10

sum1=sum1+rem

n1=int(n1/10)

return(sum1)

#MAIN PROGRAM

n=int(input("Enter any number:"))

ans1=calsum(n)

print("sum of all digits=",ans1)

Output of 2​nd​ program :


Enter any number:123

sum of all digits= 6

#3. Write a function to find the largest element in a list.


def largest(l2):
lar=0
for i in l2:
if(i>lar):
lar=i
return(lar)

# main program

l1=[]
n=int(input("Enter how many number of elements you want to store:"))
for i in range(n):
a=int(input("Enter element:"))
l1.append(a)
ans1=largest(l1)
print("Largest element:",ans1)

OUTPUT OF PRACTICAL-3
Enter how many number of elements you want to store:5

Enter element:1

Enter element:5

Enter element:6

Enter element:2

Enter element:3

Largest element: 6
# 4.Write a function to count total number of upper case, lower case letter,
digits and special symbol in a string
def trial(str2):

lwr=upr=dig=ss=0

for i in str2:

n=ord(i)

if(n>=65 and n<=90):

upr=upr+1

elif(n>=97 and n<=122):

lwr=lwr+1

elif(n>=48 and n<=57):

dig=dig+1

else:

ss=ss+1

return(upr,lwr,dig,ss) # returning four values

str1=input("enter any string:")

ans1,ans2,ans3,ans4=trial(str1)

print("Total upper case letter=",ans1)

print("Total lower case letter=",ans2)

print("Total digits letter=",ans3)

print("Total special symbol =",ans4)


OUTPUT OF PROGRAM-4
enter any string:RAJesh#123

Total upper case letter= 3

Total lower case letter= 3

Total digits letter= 3

Total special symbol = 1


#5. Write a function to print all prime numbers upto N.

# Python program to display all the prime numbers within an interval

def prime(x):

for i in range(2,x+1):

for j in range(2, i):

if (i% j ==0):

break

if(j+1==i):

print(i)

n=int(input("Enter any number:"))

prime(n)
OUTPUT OF THE PROGRAM-5
Enter any number:15

11

13
# 6. Read a text file line by line and display each line starting with letter
with ‘T’
f=open("first.txt","r")

text=f.read()

print("Original contents of text file:")

print(text)

n=0

for i in text:

if(i[0]=='T'):

n=n+1

print("Total lines:",n)
OUTPUT OF THE PROGRAM-6

Original contents of text file:


The aim of the class project is to create something that is tangible.

This should be done in groups of two to three students.

It should be started by students at least 6 months before the submission deadline.

The aim here is to find a real world problem that is worthwhile to solve.

Students are encouraged to visit local businesses and ask them about the problems that
they are facing.

Total lines: 3
# 7. program to count total vowel and consonant in a string
f=open("first.txt","r")

a=f.read()

print("original contents of the file:")

print(a)

vow=cons=upr=lwr=0

for i in a:

if(i=='a' or i=='e' or i=='o' or i=='u' or i=='i'):

vow=vow+1

else:

cons=cons+1

if(ord(i)>=65 and ord(i)<=90):

upr=upr+1

elif(ord(i)>=97 and ord(i)<=122):

lwr=lwr+1

print("Total vowels=",vow)

print("Total consonant=",cons)

print("Total upper case letters=",upr)

print("Total lower case =",lwr)


Output of the practical-7
original contents of the file:

The aim of the class project is to create something that is tangible.

This should be done in groups of two to three students.

It should be started by students at least 6 months before the submission deadline.

The aim here is to find a real world problem that is worthwhile to solve.

Students are encouraged to visit local businesses and ask them about the problems that
they are facing.

Total vowels= 115

Total consonant= 274

Total upper case letters= 5

Total lower case = 306


# 8. PROGRAM TO MODIFY A RECORD IN A TEXT FILE
import os

f1=open("student.txt","r")

f2=open("temp.txt","w")

print("Original records:")

for i in f1:

print(i)

rn1=input("Enter the roll number you want to modify:")

ans=0

f1.seek(0)

for i in f1:

rec=i.split()

cnt=rec.count(rn1)

if(cnt==1):

ans=1

print("This record will be modified:",i)

rn=input("Enter new roll number:")

name=input("Enter new name:")

add=input("Enter new address:")

rec1=rn+" "+name+" "+add+"\n"

f2.write(rec1)

else:
f2.write(i)

if(ans==0):

print("Sorry record not found")

f1.close()
output of program-8
Original records:

222 sanjay mora

103 chetan adajan

444 yogiraj ramnagar

106 manish adajan

107 manu ntpc

Enter the roll number you want to modify:103

This record will be modified: 103 chetan adajan

Enter new roll number:333

Enter new name:sunil

Enter new address:adajan


#9. Write a program to search a record in a binary file using a roll number,
if not found display appropriate message.
f=open("binary3.dat","rb")

print("Original records:")

for i in f:

rec=i.decode()

print(rec)

f.seek(0)

rn=input("Enter roll number you want to search:")

ans=0

for i in f:

rec=i.decode()

list1=rec.split()

cnt=list1.count(rn)

if(cnt==1):

print(rec)

ans=1

if(ans==0):

print("Sorry not found")

f.close()
Output of program-9
Original records:

102 babloo civiline

104 dinesh adajan

105 ejaj mora

101 SUNIL NTPC

102 ANIL ONGC

Enter roll number you want to search:101

101 SUNIL NTPC


#13.Write a menu driven program to implement a stack in python.
s=[]

while(1):

print("\n\t\t\tSTACK Operation ")

print("\t= = = = = = = = = = == = = = = = = = = = = = = = = = = = = = = = = = = = = =")

print("\t\t\t1. Display STACK ")

print("\t\t\t2. PUSH")

print("\t\t\t3. POP")

print("\t\t\t4. Exit.")

print("\t= = = = = = = = = = == = = = = = = = = = = = = = = = = = = = = = = = = = = =")

print("\t\t")

ch=int(input("\t\t\tEnter your choice:"))

if(ch==1):

len1=len(s)

print("length=",len1)

if(len1==0):

print("STACK is empty")

else:

print(s)

if(ch==2):

n=int(input("Enter an item:"))

s.append(n)
print("Element inserted:",n)

print("Top Element=",n)

if(ch==3):

len1=len(s)

print("length=",len1)

if(len1==0):

print("STACK is empty")

else:

print("Element deleted:",s.pop())

if(ch==4):

print("\n"*50)

print("\n\n\n\t\t\tThank u and have a nice day")

input()

break
​Output of program-13 :

STACK Operation

= = = = = = = = = = == = = = = = = = = = = = = = = = = = = = = = = = = = = =

1. Display STACK

2. PUSH

3. POP

4. Exit.

= = = = = = = = = = == = = = = = = = = = = = = = = = = = = = = = = = = = = =

Enter your choice:1

length= 0

STACK is empty

STACK Operation

= = = = = = = = = = == = = = = = = = = = = = = = = = = = = = = = = = = = = =

1. Display STACK

2. PUSH

3. POP

4. Exit.

= = = = = = = = = = == = = = = = = = = = = = = = = = = = = = = = = = = = = =
Enter your choice:2

Enter an item:10

Element inserted: 10

Top Element= 10

STACK Operation

= = = = = = = = = = == = = = = = = = = = = = = = = = = = = = = = = = = = = =

1. Display STACK

2. PUSH

3. POP

4. Exit.

= = = = = = = = = = == = = = = = = = = = = = = = = = = = = = = = = = = = = =

Enter your choice:1

length= 1

[10]
#15. Write a method/function SHOWWORDS() in python to read lines
#from a text file lesson.txt, and display those words, which are more than 10 characters.

Def fileread():

f=open("first.txt","r")

a=f.read()

print("original contents of the file:")

print(a)

l1=a.split()

for i in l1:

if(len(i)>=10):

print(i)

f.close()
Output of program-15

original contents of the file:


The aim of the class project is to create something that is tangible.

This should be done in groups of two to three students.

It should be started by students at least 6 months before the submission deadline.

The aim here is to find a real world problem that is worthwhile to solve.

Students are encouraged to visit local businesses and ask them about the problems that they
are facing.

submission

worthwhile

encouraged

businesses
# 12. Write a random number generator that generates random numbers between 1 and
6 (simulates a dice).

# importing the random module

import random

print("Number generated between( 1 to 6)")

for i in range(100):

print(random.randint(1,6), end=" ")


Output of the program 12:

Number generated between( 1 to 6)

6632655531545663166346221342336523214615133521626214
136345164655363322425621461563345361664356526336

You might also like