Cs Practical Final
Cs Practical Final
Date: ________________
Teacher Signature:
______________________
Acknowledgment
I would like to express my special
thanks of gratitude to my teacher
Neeta Mam who gave me the golden
opportunity to work on this practical,
which has helped me learn many
things. I am also thankful to my parents
and friends for their support.
Additionally, I would like to thank the
authors of the book Sumita Arora and
the various online resources like
Google, which provided valuable
information and assistance.
Index
1. PROGRAM FOR FACTORIAL OF A NUMBER.
2. PROGRAM TO CHECK WHETHER THE GIVEN NUMBER IS PRIME OR NOT
3. PROGRAM TO FIND THE CONSONANTS AND VOWELS IN THE GIVEN STRING
4. PROGRAM TO CHECK NO. IS PERFECT OR NOT.
5. PROGRAM TO CHECK NO. IS PALINDROME AND PERFECT NUMBER.
6. PROGRAM TO CHECK NO. IS PERFECT NUMBER.
7. PROGRAM TO FIND HAPPY NUMBER
8. PROGRAM TO CHECK GIVEN STRING IS PALINDROME.
9. PROGRAM FOR SORTING THE LIST FROM BUBBLE SORT.
10. PROGRAM FOR SORTING THE LIST USING INSERTION SORT.
11. PROGRAM FOR EMP DETAIL IN A TEXT FILE.
12. PROGRAM FOR WRITING DATA IN TEXT FILE.
13. PROGRAM FOR READING DISPLAYING THE DATA FROM BINARY FILE.
14. PROGRAM TO DISPLAY THE LIST IN REVERSE ORDER
15. PROGRAM FOR STORING THE EMP DETAIL IN BINARY FILE.
16. PROGRAM FOR READ DATA FROM CSV FILE.
17. PROGRAM TO SHOW PUSH AND POP OPERATION USING STACK.
18. PROGRAM TO ESTABLISH THE CONNECTION WITH MYSQL AND SHOW DATA.
19. MENU DERIVING PROGRAM WITH SQL CONNECTION.
20. SQL PROGRAM-1
21. SQL PROGRAM-2
22. SQL PROGRAM-3
23. SQL PROGRAM-4
24. SQL PROGRAM-5
PROGRAM TO CALCULATE FACTORIAL OF A NUMBER
def fact():
fact=1
if numb<0:
elif numb==0:
print("FACTORIAL OF 0 IS 1")
else:
for x in range(1,numb+1):
fact=fact*x
numb=int(input("ENTER A NUMBER:"))
fact()
PROGRAM TO CHECK WHETHER THE GIVEN NUMBER IS PRIME OR
NOT
def isprime(number):
if number<=1:
return False
for i in range(2,number):
if number%i==0:
return False
return True
number = int(input("enter a number"))
if isprime(number):
print("prime number")
else:
print("not prime number")
PROGRAM TO FIND THE CONSONANTS AND VOWELS IN THE GIVEN
STRING
def count_vowels_consonants(s):
vowels = "AEIOUaeiou"
vowel_count = 0
consonant_count = 0
for char in s:
if char.isalpha():
if char in vowels:
vowel_count += 1
else:
consonant_count += 1
a = input("enter a string")
print(count_vowels_consonants(a))
PROGRAM TO CHECK THE NUMBER IS PALINDROME OR NOT
def is_palindrome(number):
return str(number) == str(number)[::-1]
number = int(input("Enter a number: "))
if is_palindrome(number):
print("is a palindrome.")
else:
oprint("is not a palindrome.")
PROGRAM TO CHECK GIVEN STRING IS PALINDROME
def palindrome():
if stri==stri[ : :-1]:
print("STRING IS PALINDROME")
else:
print("STRING IS NOT PALINDROME")
stri=input("ENTER A STRING")
palindrome()
PROGRAM TO FIND HAPPY NUMBER
def sum_of_squares(n):
total = 0
while n > 0:
digit = n % 10
total += digit ** 2
n = n // 10
return total
def is_happy_number(n):
seen_numbers = []
seen_numbers.append(n)
n = sum_of_squares(n)
if n == 1:
return True
else:
return False
num = int(input("Enter a number: "))
if is_happy_number(num):
else:
def is_perfect(nu8r):
sum_of_divisors = 0
if number % i == 0:
sum_of_divisors += i
if is_perfect(number):
else:
def bubble_sort(list_1):
for i in range(0,len(list_1)-1):
for j in range(len(list_1)-1):
if(list_1[j]>list_1[j+1]):
list_1[j],list_1[j+1]=list_1[j+1],list_1[j]
return list_1
list_1=[100, 30, 10, 12, 45, 2, 7]
print("THE UNSORTED LIST IS:",list_1)
print("THE SORTED LIST IS:",bubble_sort(list_1))
PROGRAM TO SORT LIST USING INSERTION SORT
def insertionsort(list_1):
n=len(list_1)
for i in range(1, n):
val= list_1[i]
j = i-1
while j >= 0 and val < list_1[j]:
list_1[j+1]=list_1[j]
j -= 1
list_1[j+1] = val
return(list_1)
list_1= [93,25,42,44,345,4]
print("THE UNSORTED LIST IS:",list_1)
print("THE SORTED LIST IS:",insertionsort(list_1))
PROGRAM TO WRITE SOME DATA IN TEXT FILE
fin=open("student.txt","w")
for i in range(3):
studentname=input("enter name of student")
fin.write(studentname)
fin.write("\n")
fin.close()
PROGRAM TO TAKE EMPLOYEE DETAILS AND STORES IT IN A TEXT
FILE
file1 = open("empdata.txt","w")
count = int(input("how many employee:"))
for i in range(count):
print("enter the deatis of employee", (i+1),"below:")
emp_id = input("enter the emp id")
emp_name = input("enter employee name")
designation = input("enter the designation")
salary = int(input("enter the salary"))
rec = emp_id+","+emp_name+","+designation+","+str(salary)+'\n'
file1.write(rec)
file1.close()
ADDING MORE DATA TO BINARY FILE
import pickle
def add_data():
f_1=open("student.dat","ab")
r_no=int(input("ROLL NUMBER"))
stu_name=input("STUDENT NAME")
stu_add=input("STUDENT ADDRESS")
print("DATA ADDED SUCCESSFULLY")
rec=[r_no,stu_name,stu_add]
pickle.dump(rec,f_1)
f_1.close()
add_data()
PROGRAM TO DISPLAY THE LIST IN REVERSE ORDER
import pickle
Names = ['first' , 'second','third','fourth','fifth']
lst = []
for i in range (-1,-5,-1):
lst.append(Names[i])
import csv
f=open("student.csv","w")
stuwriter=csv.writer(f)
stuwriter.writerow(["ROLL NO.","NAME","MARKS"])
n=int(input("enter no. of student record to be added"))
for i in range (n):
print("STUDENT RECORD",(i+1))
rollno=int(input("enter roll no.:"))
name=input("enter name:")
marks=float(input("enter marks:"))
sturec=[rollno,name,marks]
stuwriter.writerow(sturec)
f.close()
PROGRAM TO WRITE, READ AND SEARCH IN CSV FILE
import csv
def write():
f=open("item.csv","w",newline="")
s_writer=csv.writer(f)
rec=[]
while True:
n=int(input("item no."))
name=input("item name")
quantity=int(input("quantity"))
price=int(input("price"))
rec.append([n,name,quantity,price])
if ch=="n":
break
for i in rec:
s_writer.writerow(i)
f.close()
def read():
f= open("item.csv","r")
print("reading csv file")
s_reader=csv.reader(f)
for i in s_reader:
print(i)
f.close()
def search():
f=open("item.csv","r")
print("seaching data")
s_reader=csv.reader(f)
found=0
for i in s_reader:
if i[0]==s:
print(i[1:])
found=1
if found==0:
print("no record")
f.close()
write()
read()
search()
STACK IMPLEMENTATION
s = []
top = None
def isempty(stk):
if(stk==[]):return True
else:return False
def push(stk,item):
global top
i = stk.append(item)
top = len(stk)-1
def pop(stk):
global top
if(isempty(stk)):return("underflow")
else:a = stk.pop()
if(len(stk)==0):top = None
else:top = len(stk)-1
return a
def peek(stk):
global top
if(isempty(stk)):return("underflow")
return stk[top]
def display(stk):
global top
if(isempty(stk)):print("underflow")
else:
top = len(stk)-1
print(stk[top],"<---top")
for i in range(top-1,-1,-1):
print(stk[i])
# #MAIN_PROGRAM_
print("stack implementation")
while True :
print("1:Push")
print("2:Pop")
print("3:Peek")
print("4:Display")
print("5:Exit")
if ch==1:
push(s,item)
elif ch == 2:
a = pop(s)
if a=="underflow":
print("underflow!Error")
else:
elif ch == 3:
a = peek(s)
if a=="underflow":
print("underflow!Error")
else:
print(a)
elif ch == 4:
display(s)
elif ch == 5:
con = input()
if con=="yes":break
else:
print("invalid input")
2.
Novowel = []
vowels = "AEIOUaeiou"
def PushNV(N):
for word in N:
voweltest = False
if letter in vowels:
voweltest = True
break
if not voweltest:
Novowel.append(word)
All = []
for i in range(5):
All.append(w)
PushNV(All)
if len(Novowel) == 0:
print("Empty Stack")
else:
while len(Novowel) != 0:
print()
1 . PROGRAM TO ESTABLISH CONNECTION WITH MYSQL AND
SHOW DATA
import mysql.connector as dh
myconnector = dh.connect(host = "localhost" , user = "root",passwd = "dhruv",database =
"dhruv")
if myconnector.is_connected():
print("succesfully connected to mysql database")
mycursor = myconnector.cursor()
mycursor.execute("select*from product")
myrec = mycursor.fetchall()
for i in myrec:
print (i)
2.
import mysql.connector as dh
myconnector = dh.connect(host = "localhost" , user = "root",passwd = "dhruv",database =
"product")
if myconnector.is_connected():
print("succesfully connected to mysql database")
mycursor = myconnector.cursor()
mycursor.execute("select*from company")
myrec = mycursor.fetchall()
for i in myrec:
print (i)
Sql Programs
Q1. In a database, there are two tables given below. Write SQL
command for the statements 1 to 4 and give the outputs also:
Table : EMPLOYEE
Table : JOB
JOBID JOBTITLE SALARY
101 President 200000
102 Vice President 125000
103 Administration Assistant 80000
104 Accounting Manager 70000
105 Accountant 65000
106 Sales Manager 80000
EMPLOYEEID NAME SALES JOBID
E1 Sumit sinha 1100000 102
E2 Vijay singh tomar 1300000 101
E3 Ajay rajpal 1400000 103
E4 Mohit ramnani 1250000 102
E5 Shailja singh 1450000 102
Creating the table:
Inserting values into table:
Table : JOB
Sno area App_date salary Retd_date dept
123 Agra 25-jan- 5000 25-jan- Marketing
2006 2026
127 Mathura 22-dec- 6000 22-dec- Finance
2006 2026
124 Agra 19-aug- 5500 19-aug- Marketing
2007 2027
125 Delhi 14-apr- 8500 14-apr- Sales
2004 2018
128 Pune 13-mar- 7500 13-mar- Sales
2008 2028
12. Show those employee name and date of birth who have served
more than 17 years as on date.
13. Show names of those who earn more than all of the employees
of sales dept.
14. Increase salary of the employees by 5% of their present salary
with hobby as music or they have completed atleast 3 years of
service.
16. Add a new tuple in the table personal essentially with hobby as
Music.
19. Create a view of personal and job details of those who have
served less than 15 years.
20. Erase the records of employee from job table whose hobby is
not sports.
Table : EMPLOYEE
Table : SALARY
Table : BOOKS
Table : ISSUED
Book_Id Quantity_Issued
T001 4
C001 5
F001 2
1. To show book name, author name and price of books of first publ.
publishers.
2. To list the names from books of text type.
6. To insert a new row in the table issued having the following data:
“F003”,1.
Table : WORKER
WNO NAME DOJ DOB GENDER DCODE
1001 Geoge K 2013-09-02 1991-09-01 Male D01
1002 Ryma Sen 2012-12-11 1990-12-15 Female D03
1003 Mohitesh 2013-02-03 1987-09-04 Male D05
1007 Anil Jha 2014-01-17 1984-10-19 Male D04
1004 Manila 2012-12-09 1986-11-14 Female D01
Sahai
1005 R Sahay 2013-11-18 1987-03-31 Male D02
1006 Jaya Priya 2014-06-09 1985-06-23 Female D05
3. To display the wno and name of those workers from the table
worker who are born between “1987-01-01” and “1991-12-01”.
4. To count and display male workers who have joined after “1986-
01-01”.
5. Select count(*), dcode from workers group by dcode having
count(*)>1;