CS Practical 5-6
CS Practical 5-6
Aim: Write a python program to create a menu driven program to perform arithmetic
operations based on users choice using functions.
Theory: Menu-Driven Program is a program that gets input from a user by showing the
options list, known as the menu, from which the user chooses their option.
Algorithm:
Step 1: Start the execution of the program.
Step 2: Input the values to the variable choice , a , b.
Step 3: if choice = 1, Calculate c = a + b, else go to step 4.
Step 4: if choice = 2, Calculate c = a - b, else go to step 5.
Step 5: if choice = 3, Calculate c = a * b, else go to step 6.
Step 6: if choice = 4, Calculate c = a / b, else go to step 7.
Step 7: print “Invalid option”
Step 8: Stop
Program:
division = a / b
print(a, "/", b, "=", division)
print("WELCOME TO A SIMPLE CALCULATOR")
while True:
print("\nMENU")
print("1. Sum of two Numbers")
print("2. Difference between two Numbers")
print("3. Product of two Numbers")
print("4. Division of two Numbers")
print("5. Exit")
choice = int(input("\nEnter the Choice: "))
if choice == 1:
print( "\nADDITION\n")
a = int( input("First Number: "))
b = int( input("Second Number: "))
add(a, b)
elif choice == 2:
print( "\nSUBTRACTION\n")
a = int( input("First Number: "))
b = int( input("Second Number: "))
subtract(a, b)
elif choice == 3:
print( "\nMULTIPLICATION\n")
a = int( input("First Number: "))
b = int( input("Second Number: "))
multiply(a, b)
elif choice == 4:
print( "\nDIVISION\n")
a = int( input("First Number: "))
b = int( input("Second Number: "))
divide(a, b)
elif choice == 5:
break
else:
print( "Please Provide a valid Input!")
Output :
MENU
1. Sum of two Numbers
2. Difference between two Numbers
3. Product of two Numbers
4. Division of two Numbers
5. Exit
ADDITION
First Number: 2
Second Number: 3
2+3=5
MENU
1. Sum of two Numbers
2. Difference between two Numbers
3. Product of two Numbers
4. Division of two Numbers
5. Exit
Result : Thus Menu-driven program is execute and successfully and the result is verified
Practical 6
Aim: Write a python program to create a menu driven program to calculate the area of circle,
rectangle and triangle based on users choice using functions.
Algorithm:
Step 1: Start the execution of the program.
Step 2: Input the value to the variable choice.
Step 3: if choice = 1, call the function circle(a) and print the area of circle, else go to step 4.
Step 4: if choice = 2, call the function rectangle(a , b) and print the area of rectangle, else go
to step 5.
Step 5: if choice = 3, call the function triangle(a ,b) and print the area of triangle, else go to
step 6.
Step 6: print “Invalid option”
Step 7: Stop
program:
def a_circle(radius):
area = 3.14 * radius * radius
print("Area of Circle:", area)
def a_rectangle(height, width):
area = height * width
print("Area of Rectangle:", area)
def a_triangle(base, height):
area=0.5* base * height
print("Area of Triangle:", area)
print("\nCALCULATE AREA")
print("1. Circle")
print("2. Rectangle")
print("3. Triangle")
print("4. Exit")
choice = int(input("Enter the Choice:"))
if choice == 1:
radius = int(input("Enter Radius of Circle:"))
a_circle(radius)
elif choice == 2:
height = int(input("Enter Height of Rectangle:"))
width = int(input("Enter Width of Rectangle:"))
a_rectangle(height, width)
elif choice == 3:
base = int(input("Enter base of triangle:"))
height = int(input("Enter base of triangle:"))
a_triangle(base, height)
elif choice == 4:
break
else:
print("Oops! Incorrect Choice.")
Output:
CALCULATE AREA
1. Circle
2. Rectangle
3. Triangle
4. Exit
Enter the Choice:1
Enter Radius of Circle:1
Area of Circle: 3.14
Result: Thus Menu-driven program is execute and successfully and the result is verified.
Practical 7
Aim : Write a python program for random number generation that generates a random
number between 1 to 6(simulates a dice).
Theory: Python defines a set of functions that are used to generate or manipulate random
numbers through the random module.
To work with random numbers, you can use the following functions from
the random module:
Algorithm:
Step 1: Start.
Step 2: import random module
Step 3: under while loop, write a random number generation that will generate number from 1
to 6 using randint().
Step 4: Print the random generated number.
Step 5: Stop
Program :
import random
n = random.randrange(1, 6)
guess = int(input(“Enter any number”))
while n!= guess:
if guess< n :
print(“too less”)
guess = int(input(“Enter any number”))
elif guess>n :
print(“too high”)
guess = int(input(“Enter any number”))
else:
break
print(“you guessed it right”)
Output:
Enter any number2
too high
Enter any number4
too high
Enter any number6
too high
Enter any number1
you guessed it right
Result:
The given program is executed successful and result is verified.
Practical 8
Aim: Write a program in python to read a text file- myfile.txt line by line and display
each word by #. If myfile.txt contains the following text
The best way to predict your future is to create it.
Theory : Python provides inbuilt functions for creating, writing, and reading files.
readlines() is used to read all the lines at a single go and then return them as each line a
string element in a list. This function can be used for small files, as it reads the whole file
content to the memory, then split it into separate lines. We can iterate over the list and strip
the newline ‘\n’ character using strip() function.
Algorithm:
Step 1: Start program execution
Step 2: open myfile.txt in readmode.
Step 3: check for loop, if the condition is true go to step 4, else go to step 7
Step 4: split words in line using split() function.
Step 5: check for loop, if the condition is true go to step 4, else go to step 7
Step 6: print the value of I and add ‘#’ symbol.
Step 7: Stop the program execution.
Program :
file1= open(“myfile.txt”, “r”)
for line in file1:
word = line.split()
for i in word:
print(i , end=’#’)
file1.close()
Output:
The#best#way#to#predict#your#future#is#to#create#it.
Result :