Xii - CS Practical Programs (Final)
Xii - CS Practical Programs (Final)
1) QUESTION:
Write a python program to take 5 subject marks as input and display the grade.
AIM:
To write a python program to take 5 subject marks as input and display the grade.
ALGORITHM:
PROGRAM:
sub1=int(input("Enter marks of the first subject: "))
sub2=int(input("Enter marks of the second subject: "))
sub3=int(input("Enter marks of the third subject: "))
sub4=int(input("Enter marks of the fourth subject: "))
sub5=int(input("Enter marks of the fifth subject: "))
average=(sub1+sub2+sub3+sub4+sub5)/5
if(average>=90):
print("Grade: A")
elif(average>=80 and average <90):
print("Grade: B")
elif(average >=70 and average <80):
print("Grade: C")
elif(average >=60 and average <70):
print("Grade: D")
else:
print("Grade: F")
INPUT:
Enter marks of the first subject: 76
Enter marks of the second subject: 70
Enter marks of the third subject: 65
Enter marks of the fourth subject: 77
Enter marks of the fifth subject: 80
1
OUTPUT:
Grade:C
RESULT:
__________________________________________________________________________
2) QUESTION:
AIM:
ALGORITHM:
PROGRAM:
#To compute prime factors of an integer
n=int(input("Enter an integer:"))
print("Factors are:")
i=1
while(i<=n):
k=0
if(n%i==0):
j=1
while(j<=i):
if(i%j==0):
k=k+1
j=j+1
if(k==2):
print(i)
i+=1
2
INPUT:
Enter an integer:6
OUTPUT:
Factors are:
2
3
RESULT:
__________________________________________________________________________
3) QUESTION:
AIM:
ALGORITHM:
OUTPUT:
Given matrix is:
[123]
[456]
[789]
Transpose matrix is:
[147]
[258]
[369]
RESULT:
Thus, the program was executed successfully.
4
4) QUESTION:
Write a python program to count and display the vowels of a given text.
AIM:
To write a python program to count and display the vowels of a given text.
ALGORITHM:
PROGRAM:
text = input(“Enter a String:”)
vowels = ‘aeiouAEIOU’
vowelcount = 0
for i in text:
if i in vowels:
vowelcount += 1
print(“Number of vowels in the given string is:” , vowelcount)
INPUT:
OUTPUT:
RESULT:
__________________________________________________________________________
5
5) QUESTION:
Write a python program to check the number is prime or not by using function.
AIM:
To write a python program to check the number is prime or not by using function.
ALGORITHM:
PROGRAM:
def isprime(n):
if(n==1):
return False
elif(n==2):
return True
else:
for x in range(2,n):
if(n%x==0):
return False
else:
return True
num=int(input("Enter a number:"))
s=isprime(num)
if(s):
print(num,"is a prime number")
else:
print(num,"is not a prime number")
INPUT:
Enter a number:14
OUTPUT:
14 is not a prime number
6
RESULT:
Thus, the program was executed successfully.
__________________________________________________________________________
6) QUESTION:
Write a python program to check whether the passed string is palindrome or not by
using user defined function.
AIM:
To write a python program to check whether the passed string is palindrome or not by
using user defined function.
ALGORITHM:
PROGRAM:
def ispalindrome(str):
leftpos=0
rightpos=len(str)-1
while(rightpos >= leftpos):
if(not(str[leftpos]== str[rightpos])):
return False
leftpos+=1
rightpos-=1
return True
str=input("Enter a string:")
if(ispalindrome(str)):
print("The Given String is Palindrome")
else:
print("The Given String is Not a Palindrome")
7
INPUT:
Enter a string:madam
OUTPUT:
RESULT:
Thus, the program was executed successfully.
__________________________________________________________________________
7) QUESTION:
Write a python program to find area of the given shape using math module.
AIM:
To write a python program to find area of the given shape using math module.
ALGORITHM:
import math
def calculate_area(name):
# converting all characters into lower cases
name = name.lower()
if name.lower()=="rectangle":
l = int(input("Enter rectangle's length:"))
b = int(input("Enter rectangle's breadth:"))
rect_area = l * b
print("The area of rectangle is:",rect_area)
8
elif name.lower() == "square":
s = int(input("Enter square's side length:"))
sqt_area = math.pow(s,2)
print("The area of square is:",sqt_area)
elif name.lower() == "triangle":
h = int(input("Enter triangle's height length:"))
b = int(input("Enter triangle's breadth length:"))
tri_area = 0.5 * b * h
print("The area of triangle is:",tri_area)
elif name.lower() == "circle":
r = int(input("Enter circle's radius length:"))
circ_area=math.pi*pow(r,2)
print("The area of circle is:",circ_area)
elif name.lower() == 'parallelogram':
b = int(input("Enter parallelogram's base length:"))
h = int(input("Enter parallelogram's height length:"))
para_area = b * h
print("The area of parallelogram is:",para_area)
else:
print("Sorry! This shape is not available")
shape_name = input("Enter the name of shape
rectangle/square/triangle/circle/parallelogram:")
calculate_area(shape_name)
INPUT:
Enter the name of shape rectangle/square/triangle/circle/parallelogram:square
Enter square's side length:4
OUTPUT:
RESULT:
Thus, the program was executed successfully.
__________________________________________________________________________
9
8) QUESTION:
AIM:
ALGORITHM:
PROGRAM:
INPUT:
Enter a number:5
OUTPUT:
The factorial of 5 is 120
RESULT:
Thus, the program was executed successfully.
__________________________________________________________________________
10
9) QUESTION:
AIM:
ALGORITHM:
PROGRAM:
punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
my_str = input("Enter the string with punctuations:")
no_punct = ""
for char in my_str:
if char not in punctuations:
no_punct = no_punct + char
print(no_punct)
INPUT:
Enter the string with punctuations:"computer*#science"
OUTPUT:
computerscience
RESULT:
Thus, the program was executed successfully.
_________________________________________________________________________
11
10) QUESTION:
Write a python program to append text to a text file and read line by line.
AIM:
To write a python program to append text to a text file and read line by line.
ALGORITHM:
PROGRAM:
INPUT:
Enter a string to add at end of file: Python
OUTPUT:
Computer Science Computer Science Computer Science Python
RESULT:
Thus, the program was executed successfully.
__________________________________________________________________________
12
11) QUESTION:
Write a python program to count number of words starting with capital letter and
number of words starting with small letter in a text file.
AIM:
To write a python program to count number of words starting with capital letter and
number of words starting with small letter in a text file.
ALGORITHM:
OUTPUT:
No. of words starting with capital letter 8
No. of words starting with small letter 2
RESULT:
Thus, the program was executed successfully.
13
12) QUESTION:
Write a python program to store students details in text file and read the same.
AIM:
To write a python program to store students details in text file and read the same.
ALGORITHM:
RESULT:
Thus, the program was executed successfully.
__________________________________________________________________________
13) QUESTION:
Write a python program to read and store details of 5 students in csv file.
AIM:
To write a python program to read and store details of 5 students in csv file.
ALGORITHM:
INPUT:
Student record 1
Enter Rollno:101
Enter Name:Alice
Enter Marks:93
Student record 2
Enter Rollno:102
Enter Name:Arjun
Enter Marks:77
Student record 3
Enter Rollno:103
Enter Name:Senthil
Enter Marks:86
Student record 4
Enter Rollno:104
Enter Name:Madhumitha
Enter Marks:69
Student record 5
Enter Rollno:105
Enter Name:Sumitha
Enter Marks:99
16
OUTPUT:
__________________________________________________________________________
14) QUESTION:
Write a python program to store and read the details of students in binary file.
AIM:
To write a python program to store and read the details of students in binary file.
ALGORITHM:
17
PROGRAM:
import pickle
stu=[ ]
stufile = open('stu.dat', 'ab')
ans = 'y'
while ans == 'y':
mark = [ ]
rno = int(input("Enter the roll number: "))
name = input("Enter name: ")
for i in range(3):
a = float(input("Enter the marks:”,(i+1)))
mark.append(a)
stu = [rno, name, mark]
pickle.dump(stu, stufile)
ans = input("Want to append more records? (Y/N): ").lower()
stufile.close()
stu = [ ]
stufile = open('stu.dat', 'rb')
try:
while True:
stu = pickle.load(stufile)
print(stu)
except EOFError:
stufile.close( )
INPUT:
Enter the roll number: 101
Enter name: Suresh
Enter the mark 1: 90
Enter the mark 2: 81
Enter the mark 3: 75
Want to append more records? (Y/N): Y
Enter the roll number: 102
Enter name: Vignesh
Enter the mark 1: 65
Enter the mark 2: 54
Enter the mark 3: 69
Want to append more records? (Y/N): N
18
OUTPUT:
[101, 'Suresh', [90, 81, 75]]
[102, 'Vignesh', [65, 54, 69]]
RESULT:
Thus, the program was executed successfully.
__________________________________________________________________________
15) QUESTION:
Write a python program to read students detail from binary file and display records of
students whose mark is greater than 81.
AIM:
To write a python program to read students detail from binary file and display records
of students whose mark is greater than 81.
ALGORITHM:
PROGRAM:
import pickle
found = False
print("Searching in file stu.dat")
with open("stu.dat", "rb") as fin:
students = pickle.load(fin)
for student in students:
if student['mark'] > 81:
print(student)
found = True
if not found:
print("No records with marks greater than 81")
19
OUTPUT:
[101, 'Suresh', [90, 81, 75]]
RESULT:
Thus, the program was executed successfully.
__________________________________________________________________________
16) QUESTION:
AIM:
ALGORITHM:
PROGRAM:
def isEmpty(stack):
return len(stack) == 0
def push(stack, item):
stack.append(item)
def pop(stack):
if isEmpty(stack):
return "underflow"
else:
return stack.pop()
def reverse(string):
n = len(string)
stack = []
for i in range(0, n, 1):
push(stack, string[i])
20
reversed_string = ""
for i in range(0, n, 1):
reversed_string += pop(stack)
return reversed_string
string = input("Enter the string to be reversed: ")
result = reverse(string)
print("Reversed string is: " + result)
INPUT:
Enter the string to be reversed: Computer
OUTPUT:
Reversed string is: retupmoC
RESULT:
Thus, the program was executed successfully.
_________________________________________________________________________
17) QUESTION:
AIM:
ALGORITHM:
OUTPUT:
Field Datatype Size Constraint
id integer 11 PRIMARY KEY
name varchar 20
salary integer 11
address varchar 30
designation varchar 30
RESULT:
Thus, the program to add a new column was executed successfully.
_________________________________________________________________________
18) QUESTION:
Write a python program to create employee table with attributes ID, name, address,
salary and insert data in it using SQL queries.
AIM:
To write a python program to create employee table with attributes ID, name, address,
salary and insert data in it using SQL queries.
22
ALGORITHM:
23
OUTPUT:
2 was inserted
(2, 'ashwin', 3000, 'mumbai')
(3, 'amith', 10000, 'mumbai')
RESULT:
Thus, the program was executed successfully.
_________________________________________________________________________
19) QUESTION:
Write a python program to read the details of employees whose salary is greater than
5000 using sql queries.
AIM:
To write a python program to read the details of employees whose salary is greater
than 5000 using sql queries.
ALGORITHM:
OUTPUT:
1 was inserted
(1, 'amith', 10000, 'mumbai')
RESULT:
Thus, the program was executed successfully.
_________________________________________________________________________
20) QUESTION:
AIM:
ALGORITHM:
OUTPUT:
2 was inserted
(2, 'ashwin', 5000, 'mumbai')
(3, 'amith', 12000, 'mumbai')
RESULT:
Thus, the program was executed successfully.
_________________________________________________________________________
26
21) QUESTION:
Write SQL query to create table ‘EMPLOYEE’ with attributes Empid, Firstname,
Lastname, Address, City with appropriate datatype and to insert 2 records into the table.
AIM:
To write SQL query to create table ‘EMPLOYEE’ with attributes Empid, Firstname,
Lastname, Address, City with appropriate datatype and to insert 2 records into the table.
SQL QUERY:
To Create table:
empid INT(3),
firstname VARCHAR(20),
lastname VARCHAR(20),
address VARCHAR(20),
city CHAR(20)
);
To Insert values:
OUTPUT:
RESULT:
__________________________________________________________________________
27
22) QUESTION:
Write Query to
(iii) List the name of the employees who got salary less than 10,000
AIM:
To write Query to Create table empsalary , insert record in table empsalary and List
the name of the employees who get less than 10,000.
SQL QUERY:
empid INT(10),
empname VARCHAR(30),
salary INT(20),
designation VARCHAR(15)
);
(iii) List name of employees who get salary less than 10,000:
(iii)
Empname
Harish
Rohan
RESULT:
__________________________________________________________________________
23) QUESTION:
Write SQL query to add foreign key constraint to the table empsalary and display the
details and salary of employee who belongs to “Chennai”.
AIM:
To write SQL query to add foreign key constraint to the table empsalary and display
the details and salary of employee who belongs to “Chennai”.
SQL QUERY:
To Create table:
empid INT(3),
empname VARCHAR(20),
address VARCHAR(20),
city CHAR(20)
);
29
(004, 'Aathi', ‘K K nagar’, 'Chennai'),
To create empsalary:
empid INT(10),
salary INT(10),
designation VARCHAR(15),
);
(005, '9000
, 'Typist');
SELECT empname,salary
OUTPUT:
30
(ii) Empsalary
Empname Empsalary
Suresh 15000
Aathi 20000
RESULT:
_________________________________________________________________________
24) QUESTION:
CLUB
(i)
Count(distinct age)
7
(ii)
Min(pay)
750
(iii)
Sum(pay)
4100
(iv)
Sports Count(Coachname)
Karate 3
Squash 2
Basket ball 2
Swimming 2
RESULT:
__________________________________________________________________________
25) QUESTION:
Find out output based on the SQL query based on student detail table.
32
(i) Select * from student order by name;
(ii) Select count(*) class from student Group by class
(iii) Select max(Mark) from Student
OUTPUT:
(i)
(ii)
Count Class
3 12
3 11
(iii)
MAX(mark)
470
RESULT:
_________________________________________________________________________
33