PSP Lab Manual1
PSP Lab Manual1
LABORATORY MANUAL
ENGINEERING COLLEGE (AUTONOMOUS)
KOMARAPALAYAM-637 303
DEPARTMENT OF
Ex. Page
Date Name of the Experiment Marks Sign
No No
Write a algorithm & draw flowchart for simple
1
computational problems
Write a program to perform different arithmetic
2
operations on numbers in python.
Write a python program to implement the various
3
control structures
Write a python program for computational problems
4
using recursive function.
Algorithm:
1. Read one input value using input function
2. Convert it into float
3. Find the square root of the given number using the formula inputvalue ** 0.5
4. Print the result
5. Exit.:
Flow Chart:
Start
X=float(A)
C=X**0.5
Stop
Program:
Output:
enter a number: 676
square root: 26.0
Output:
32.0
Result
Thus the Flowchart for to find square root of a number is executedsuccessfully and
output is verified.
1b. Compute the GCD of two
numbers Aim:
Algorithm:
Flow Chart:
Start
Print Result
Stop
Finding HCF of a number:
a = int(input("Enter the first number: "))
b = int(input("Enter the second number: "))
HCF = 1
for i in range(2,a+1):
if(a%i==0 and b%i==0):
HCF = i
print("First Number is: ",a)
print("Second Number is: ",b)
print("HCF of the numbers is: ",HCF)
Output:
Result:
Thus the flowchart for compute GCD of two numbers is executed successfully and
outputis verified.
Exp No.2 Write a program to perform different arithmetic operations on numbers in python
Aim:
Algorithm:
Step1: Start
Step2: Read the input num1, num2
Step3: calculate the addition, subtraction, multiplication, division from Inputs
Step4: Display the Result
Step6: Stop
Program:
num1 = float(input(" Please Enter the First Value Number 1: "))
num2 = float(input(" Please Enter the Second Value Number 2:
"))
Output:
>>>
============== RESTART: C:/Users/students/Desktop/arithmetic.py ==============
Please Enter the First Value Number 1: 10
Please Enter the Second Value Number 2: 30
The Sum of 10.0 and 30.0 = 40.0
The Subtraction of 30.0 from 10.0 = -20.0
The Multiplication of 10.0 and 30.0 = 300.0
The Division of 10.0 and 30.0 = 0.3333333333333333
The Modulus of 10.0 and 30.0 = 10.0
The Exponent Value of 10.0 and 30.0 = 1e+30
>>>
Result:
Thus the program to perform different arithmetic operations on numbers is executed
successfully and outputis verified.
Exp No.3 Write a python program to implement the various control structures
Aim:
To find a maximum value in a given list using python program
Algorithm:
STEP 1 Start the program
STEP 2 Read the no of terms in the list as
n STEP 3 Read the list values from 1 to n
STEP 4 Initialize max =0
STEP 5 Initialize i=1 repeat step 6 until n
STEP 6 If list[i]> max
STEP 7 Set max = list[i]
STEP 8 Print Maximum numbers as max
STEP 9 Stop the program.
Program:
#input size and elments of an array
n=int(input("Enter no of terms:"))
list1=[]
print("Enter the Numbers\n")
for i in range(n):
list1.append(int(input()))
#initialize the greatest value
Max =0
#Find the greatest value
for i in range(n):
if list1[i]>Max:
Max=list1[i]
# Print the greatest Number
print("Greatest Number is ", Max)
Output
Result:
Thus the program to find the maximum value in a list is executed successfully and
output isverified.
Exp No.4 Write a python program for computational problems using recursive function
Aim:
To write python program to find Factorial of Number using recursion.
Algorithm:
1. Start the program.
2. Read the number to find the factorial.
3. Declare the recursive function recur_factorial (n).
4. In function fact(n), do the computation n*recur_factorial(n-1)
5. Repeat the step3 until the value n becomes 0.
6. Display the value of n*recur_factorial(n-1) returned from the function recur_factorial (n).
7. Stop the program.
Program:
def recur_factorial(n):
if n == 1:
return n
else:
return n*recur_factorial(n-1)
# take input from the user
num = int(input("Enter a number: "))
# check is the number is negative
if num < 0:
print("Sorry, factorial does not exist for negative
numbers") elif num == 0:
print("The factorial of 0 is
1") else:
print("The factorial of",num,"is",recur_factorial(num))
Output:
Result:
Thus the program for computational problems using recursive function is executed
successfully and outputis verified.
Exp No.5 Demonstrate use of list for data validation
Aim:
To demonstrate use of list for data validation.
Program:
Adding Element to a List
# animals list
animals = ['cat', 'dog', 'rabbit']
# 'guinea pig' is appended to the animals
list animals.append('guinea pig')
Output
# animals list
animals = ['cat', 'dog',
'rabbit'] # list of wild animals
wild_animals = ['tiger', 'fox']
# appending wild_animals list to the animals
list animals.append(wild_animals)
Output
# random list
random = ['a', ('a', 'b'), ('a', 'b'), [3, 4]]
# count element ('a', 'b')
count = random.count(('a', 'b'))
# print count
print("The count of ('a', 'b') is:", count)
# count element [3, 4]
count = random.count([3, 4])
# print count
print("The count of [3, 4] is:", count)
Output
# vowels list
vowels = ['a', 'e', 'i', 'o', 'i',
'u'] # index of 'e' in vowels
index = vowels.index('e')
print('The index of e:',
index) # element 'i' is
searched
# index of the first 'i' is
returned index =
vowels.index('i') print('The
Output
The index of e:
1 The index of i:
2
Result:
Thus the program for the use of list for data validation is executed successfully and
outputis verified.
Exp No.6 Develop a python program to explore string functions to print reverse words of string
Aim:
To develop a python program to explore string functions to print reverse words of string.
Algorithm:
1. Initialize the string.
2. Split the string on space and store the resultant list in a variable called word.
3.Reverse the list words using reversed function.
4. Convert the result to list.
Program:
def rev_sentence(sentence):
# then reverse the split string list and join using space
reverse_sentence = ' '.join(reversed(words))
Result:
Thus the program for the use of list for data validation is executed successfully
and
outputis verified.
Exp No.7a Implement linear search and binary search
Aim:
Algorithm:
4. Else increment the position and repeat step 3 until pos reaches the length of the list
Program:
def search(alist,item):
pos=0
found=False stop=False
if alist[pos]==item:
found=True
else:
if alist[pos]>item:
stop=True
else:
pos=pos+1
return found
a=[]
n=int(input("enter upper limit"))
for i in range(0,n):
e=int(input("enter the elements"))
a.append(e)
x=int(input("enter element to search"))
search(a,x)
Output:
Result:
Thus the Python Program to perform linear search is executed successfully and the
output is verified.
Exp No.7b Implement binary search
Aim:
Algorithm:
Program:
def bsearch(alist,item):
first=0
last=len(alist)-1
found=False
while first<=last and not found:
mid=(first+last)//2
if alist[mid]==item:
found=True
print("element found in position",mid)
else:
if item<alist[mid]:
last=mid-1
else:
first=mid+mid-1
return found
a=[]
n=int(input("enter upper
limit"))
for i in range(0,n):
e=int(input("enter the elements"))
a.append(e)
x=int(input("enter element to search"))
bsearch(a,x)
Output:
Result:
Thus the Python Program to perform binary search is executed successfully and the
output is verified.
Exp No.8 Develop a python program to implement sorting methods
Aim:
Algorithm:
2. Initialise pos=0
4. Set pos=location
Program:
def selectionSort(alist):
for i in range(len(alist)-1,0,-1):
pos=0
for location in range(1,i+1):
if alist[location]>alist[pos]:
pos= location
temp = alist[i]
alist[i] = alist[pos]
alist[pos] = temp
alist = [54,26,93,17,77,31,44,55,20]
selectionSort(alist)
print(alist)
Output:
Result:
Thus the Python Program to perform selection sort is successfully executed and the
output is verified.
Exp No.9 Develop python programs to perform operations on dictionaries
Aim:
To develop python programs to perform operations on dictionaries.
Program:
Output:
Result:
Thus the program to perform operations on dictionaries is executed successfully and
outputis verified.
Exp No.10 Write a python program to read and write into a file
Aim:
To write a Python program to find the most frequent words in a text read from a file.
Algorithm:
Program:
# Program to show various ways to read and
# write data in a file.
file1.write("Hello \n")
file1.writelines(L)
file1.close()
file1 = open("myfile.txt","r+")
print (file1.read())
print
# seek(n) takes the file handle to the nth
file1.seek(0)
print (file1.readline())
file1.seek(0)
print (file1.read(9))
file1.seek(0)
print (file1.readline(9) )
file1.seek(0)
# readlines function
print (file1.readlines() )
file1.close()
Output:
Hello
This is Delhi
This is Paris
This is London
is Hello
Hello
Th
Hello
['Hello \n', 'This is Delhi \n', 'This is Paris \n', 'This is London \n']
>>>
Result:
Thus the program to perform operations on dictionaries is executed successfully and
outputis verified.