0% found this document useful (0 votes)
12 views

CS Practicals Exp 1,6,9,11,14

The document contains multiple programming examples demonstrating basic operations such as addition, subtraction, multiplication, and division in a calculator, as well as file handling for student and employee records using CSV and pickle. It also includes a stack implementation using lists with push, pop, and show functionalities. Each example is accompanied by source code and sample output.
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)
12 views

CS Practicals Exp 1,6,9,11,14

The document contains multiple programming examples demonstrating basic operations such as addition, subtraction, multiplication, and division in a calculator, as well as file handling for student and employee records using CSV and pickle. It also includes a stack implementation using lists with push, pop, and show functionalities. Each example is accompanied by source code and sample output.
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/ 15

Exp.no.

01 :

SOURCE CODE

def addition(a, b):

sum = a + b

print(a, "+", b, "=", sum)

def subtraction(a, b):

difference = a - b

print(a, "-", b, "=", difference)

def multiplication(a, b):

product = a * b

print(a, "*", b, "=", product)

def divide(a, b):

quotient = a / b

remainder = a % b

print("Quotient of", a, "/", b, "=", quotient)

print("Remainder of", a, "%", b, "=", remainder)

print("WELCOME TO CALCULATOR")

while True:

print("\nChoose the operation to perform:")

print("1. Addition of two numbers")


print("2. Subtraction of two numbers")

print("3. Multiplication of two numbers")

print("4. Division of two numbers")

print("5. Exit")

choice = int(input("\nEnter your Choice: "))

if choice == 1:

print("\nAddition of two numbers")

a = int(input("Enter the first number: "))

b = int(input("Enter the second number: "))

addition(a,b)

elif choice == 2:

print("\nSubtraction of two numbers")

a = int(input("Enter the first number: "))

b = int(input("Enter the second number: "))

subtraction(a,b)

elif choice == 3:

print("\nMultiplication of two numbers")

a = int(input("Enter the first number: "))

b = int(input("Enter the second number: "))

multiplication(a,b)
elif choice == 4:

print("\nDivision of two numbers")

a = int(input("Enter the first number: "))

b = int(input("Enter the second number: "))

divide(a,b)

elif choice == 5:

print("Thank You! Bye.")

break

else:

print("Invalid Input! Please, try again.")

OUTPUT

Choose the operation to perform:

1. Addition of two numbers

2. Subtraction of two numbers

3. Multiplication of two numbers

4. Division of two numbers

5. Exit

Enter your Choice: 1


Addition of two numbers

Enter the first number: 2

Enter the second number: 2

2+2=4

Choose the operation to perform:

1. Addition of two numbers

2. Subtraction of two numbers

3. Multiplication of two numbers

4. Division of two numbers

5. Exit

Enter your Choice: 2

Subtraction of two numbers

Enter the first number: 4

Enter the second number: 2

4-2=2

Choose the operation to perform:


1. Addition of two numbers

2. Subtraction of two numbers

3. Multiplication of two numbers

4. Division of two numbers

5. Exit

Enter your Choice: 3

Multiplication of two numbers

Enter the first number: 3

Enter the second number: 3

3*3=9

Choose the operation to perform:

1. Addition of two numbers

2. Subtraction of two numbers

3. Multiplication of two numbers

4. Division of two numbers

5. Exit

Enter your Choice: 4


Division of two numbers

Enter the first number: 20

Enter the second number: 5

Quotient of 20 / 5 = 4.0

Remainder of 20 % 5 = 0

WELCOME TO CALCULATOR

Choose the operation to perform:

1. Addition of two numbers

2. Subtraction of two numbers

3. Multiplication of two numbers

4. Division of two numbers

5. Exit

Enter your Choice: 5

Thank You! Bye.

Exp.no.06 :

SOURCE CODE
def showlines():

with open("story.txt",'r') as F:

s=F.read()

for w in s:

if w.endswith('.') or w.endswith('?') or w.endswith('!'):

​ print(w)

elif w=='\n':

print(end=" ")

else:

print(w,end=" ")

showlines()

OUTPUT:

our parents told us that we must eat vegetables


t o b e​ healthy.

And its turns out,our parents were right!

S o , w h a t e l s e​ d i d o u r p a r e n t s t e l l ?

Exp.no.09 :

SOURCE CODE
importpickle

def Create():
F=open("Students.dat",'ab') opt='y'
whileopt=='y':
Roll_No=int(input("Enterrollnumber:")) Name=input("Enter name:")
L=[Roll_No,Name] pickle.dump(L,F)
opt=input("doyouwanttoaddanotherstudentdetails(y/n):") F.close()

def search():
F=open("Students.dat",'rb')
no=int(input("enterRoll.Notosearch:")) found=0
try:
whileTrue:
S=pickle.load(F) if S[0]==no:
print("ThesearchedROll.Noisfoundanddetailsare:",S) found=1
break
except:
F.close()
if found==0:
print("ThesearchedRoll.Noisnotfound")

#MAINPROGRAM

Create()

search()
OUTPUT:

Enter rollnumber:1

Enter name:abc

Do you want to add another student details (y/n): y


Enter roll number:2
Enter name:cde

Do you want to add another student details(y/n):n

enter Roll.No to search:2

The searched ROll.No is found and details are: [2,'def']

Exp.no.11 :

SOURCE CODE

import csv

def Create():

F=open("Emp.csv",'w',newline='\n')

w=csv.writer(F)

opt='y'
while opt=='y':

No=int(input("Enter Employee Number:"))

Name=input("Enter emp name:")

sal=float(input("enter emp salary:"))

L=[No,Name,sal]

w.writerow(L)

opt=input("Do you want to continue(y/n)?")

F.close()

def Search():

F=open("Emp.csv",'r',newline='\r\n')

no=int(input("Enter emp number to search:"))

found=0

row=csv.reader(F)

for data in row:

if data[0]==str(no):

​print("\nEmployee Details are:")

print("====================")

​print("Name:",data[1])

​print("salary:",data[2])

print("====================")
​found=1

​break

if found==0:

​ print("not found")

F.close()

Create()

Search()

OUTPUT:

Enter Employee Number:1

Enter emp name:abc

enter emp salary:12000

Do you want to continue(y/n)?y

Enter Employee Number:2

Enter emp name:def

enter emp salary:15000

Do you want to continue(y/n)?n

Enter emp number to search:3

not found
Enter Employee Number:1

Enter emp name:abi

enter emp salary:12000

Do you want to continue(y/n)?y

Enter Employee Number:2

Enter emp name:nandha

enter emp salary:15000

Do you want to continue(y/n)?y

Enter Employee Number:3

Enter emp name:akash

enter emp salary:18000

Do you want to continue(y/n)?n

Enter emp number to search:2

Employee Details are:

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

Name: nandha

salary: 15000.0

====================
Exp.no.14 :

SOURCE CODE

def push(Arr,item) :
if item%5 == 0 :
Arr.append(item)

def pop(Arr):
if Arr==[] :
print("No item found")
else :
print("The deleted element :")
return Arr.pop()
def show():
if Arr==[] :
print("No item found")
else :
top = len(Arr)- 1
print("The stack elements are :")
for i in range(top,-1,-1) :
print(Arr[i] )

#Main program
Arr = []
top = None
while True :
print("*****STACK IMPLEMENTATION USING LIST*****")
print("1: PUSH")
print("2: Pop")
print("3: Show")
print("4: Exit")
ch = int(input("Enter ur choice :"))
if ch==1:
val=int(input("enter no to push :"))
push(Arr,val)
elif ch==2:
res = pop(Arr)
print(res)
elif ch==3:
show()
elif ch==0:
print("Bye")
break

OUTPUT :

*****STACK IMPLEMENTATION USING LIST*****


1: PUSH
2: Pop
3: Show
4: Exit
Enter ur choice :1
enter no to push :55
*****STACK IMPLEMENTATION USING LIST*****
1: PUSH
2: Pop
3: Show
4: Exit
Enter ur choice :1
enter no to push :33
*****STACK IMPLEMENTATION USING LIST*****
1: PUSH
2: Pop
3: Show
4: Exit
Enter ur choice :2
The deleted element :
55
*****STACK IMPLEMENTATION USING LIST*****
1: PUSH
2: Pop
3: Show
4: Exit
Enter ur choice :3
No item found
*****STACK IMPLEMENTATION USING LIST*****
1: PUSH
2: Pop
3: Show
4: Exit
Enter ur choice :0
Bye

You might also like