0% found this document useful (0 votes)
4 views66 pages

PSPP Lab Manual Pgms

The document outlines various programming exercises, including algorithms and flowcharts for calculating electricity bills, retail shop billing, sine series, weight of a steel bar, electrical current in a three-phase AC circuit, and value exchanges between variables. Each exercise includes a structured approach with algorithms, programs, flowcharts, and sample outputs demonstrating successful execution. The document serves as a comprehensive guide for implementing basic programming concepts using Python.
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)
4 views66 pages

PSPP Lab Manual Pgms

The document outlines various programming exercises, including algorithms and flowcharts for calculating electricity bills, retail shop billing, sine series, weight of a steel bar, electrical current in a three-phase AC circuit, and value exchanges between variables. Each exercise includes a structured approach with algorithms, programs, flowcharts, and sample outputs demonstrating successful execution. The document serves as a comprehensive guide for implementing basic programming concepts using Python.
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/ 66

Ex. No.

1(a) Electricity Billing

Date:

Aim:

To write an algorithm and develop a flowchart to calculate electricity bill

Algorithm:

Step 1: Start the program

Step 2: Read the number of units(n)

Step 3: A) If n == 0 to 100 then bill amount BA=0

B) If n==101 to 200 then calculate bill amount using

Formula = (n-100)*1.5

C) If n>200 then calculate bill amount using the below

Formula BA=100*2+(n-200)*3

Step 4: Print electricity bill amount

Step 5: Stop the program

Program:

n = int(input(“Enter number of units”)

if(n<100):

BA= 0

elif(n<=200):

BA=(n-100)*1.5

elif(n>200):

BA=100*2+(n-200)*3

print(“Electricity Bill =”, BA)


Flowchart:

Start

Read number of units (n)

Yes
If n<=100
? BA=0

No

If Yes
(n>100&n<= BA= BA=(n-
200) ? 100)*1.5

No

Yes
If (n>200)?
BA =100*2+(n-
200)*3

No

Print Bill amount

Stop
Output:

Enter number of units: 50

Electricity Bill: 0

Result:

Thus an algorithmtocalculate electricity bill was written and the flowchart was
developed successfully.
Ex. No. 1(b) Retail Shop Billing

Date:

Aim:

To write an algorithm and draw flowchart to calculate Retail shop billing

Algorithm:

Step 1:Start the program

Step 2:Read the number of items (n) purchased

Step 3:Initialize Bill_amount = 0, i=1

Step 4:Read the item name(name), item quantity(Qt) , item price(P)

Step 5:Calculate Bill_amount = Bill_amount + (Qt*P)

Step 6:Repeat step 4 and 5 until i<=n

Step 7:Print cost

Step 8: Stop the program

Program:

n = int(input(“Enter number of items”)

Bill_amount = 0,

i=1

for i in range(n):

item_name = int(input(“Enter item name”)

Qt = int(input(“Enter item Quantity”)

P=int(input(“Enter item Price”)

Bill_amount = Bill_amount + (Qt*P)

Print(“Total Bill=”, Bill_amount)


Flowchart:

Start

Read the number of items n

Initialize Bill_amount = 0, i=1

No
i<=n?

Yes

Read item name, Quantity(Qty), Price(P)

Calculate Bill_amount =
Bill_amount + (Qt*P)

Print Bill_amount

Stop
Output:

Enter number of items: 2

Enter item name : Apple

Enter item Quantity : 2

Enter item Price: 100

Enter item name : orange

Enter item Quantity : 1

Enter item Price: 50

Total Bill= 250

Result:

Thus an algorithmto calculate Retail shop bill was written and the flowchart was
developed successfully.
Ex. No. 1(c) Sin Series

Date:

Aim:

To write an algorithm and develop flowchart to implement sine series

Algorithm:

Sin(X) = (X^1/1!)-(X^3/3!)+(X^5/5!)-(X^7/7!)+…….

Step 1: Start the program

Step 2: Initialize i=1

Step 3: Read the value of x//

Step 4: Assign y=x

Step 5: Read the value of n //ie: the range of sine series

Step 6: Set x=x*3.14159/180,

t=x,

sum=x

Step 7: Calculate t=t(*(-1)*x*x)/2*i*(2*i+1))

Step 8: Assign sum= sum+t

Step 9: Increment i value

Step 10: Repeat steps 7 to 9 unit i<=n

Step 11: Print the value of sine (y)

Step 12: Display sum

Step 13: Stop the program


Program:

import math

def sin(x,n):

sine = 0

foriin range(n):

sign = (-1)**i

pi=22/7

y=x*(pi/180)

sine = sine + ((y**(2.0*i+1))/math.factorial(2*i+1))*sign

return sine

x=int(input("Enter the value of x in degrees:"))

n=int(input("Enter the number of terms:"))

print(round(sin(x,n),2))
Flowchart:
Output:

Enter the value of x in degree: 30

Enter the number of terms: 10

0.5

Result:

Thus an algorithmto implement the sin series was written and the flowchart was
developed successfully.
Ex. No. 1(e) Weight of a Steel Bar

Date:

Aim:

To write an algorithm and draw flowchart to calculate weight of a steel bar

Algorithm:

Step 1: Startthe program

Step 2: Read the values of Density of the steel bar D

Step 3: Read the values of Steel Volume V

Step 4: Calculate Weight using W = D*V

Step 5: Print Weight of the Steel Bar W

Step 6: Stopthe program

Program:

D = float(input(“Enter the Density of the steel bar”))

V= float(input(“Enter the Volume of the steel bar”))

W = D*V

print (“Weight of the Steel Bar =”, W)


Flowchart:

Start

Read the value of Density D

Read the values of Steel Volume V

Calculate Weight
W =D*V

Print Weight of the Steel Bar W

Stop

Output:

Enter the Density of the steel bar: 1000

Enter the length of the steel bar: 10

Weight of the Steel Bar = 10.0kg

Result:

Thus an algorithmto calculate weight of a steel bar was written and the flowchart was
developed successfully.
Ex. No. 1(f) Compute Electrical Current in Three Phase AC Circuit

Date:

Aim:

To write an algorithm and develop flowchart to compute electrical current in three


phase AC circuit

Algorithm:

P = √3 x VL x IL x Cos ФTherefore I = P/ (√3x VL xCos Ф)

Step 1: Start the program

Step 2: Read the values of Power (P), Voltage (V)

Step 3: Initialize power factor(pf) = 1

Step 4: Calculate Electrical Current in Three Phase AC Circuit using

I = P/ (√3 x VL x Cos Ф)

P = Power in Watts V = Voltage in Volts I = Current in Amperes

Cos Ф = Power Factor assume Cos Ф=1

Step 5: Calculate Electrical Current in Three Phase AC Circuit (I)

Step 6: Stop the program

Program:

P = float(input(“Enter the power value”))

V = float(input(“Enter the Voltage value”))

PF = 1

I = P/ (√3 x VL x PF)

print(“Electrical Current in Three Phase AC Circuit”, I)


Flowchart:

Start

Read the values of Power (P),


Voltage (V)

Calculate Electrical Current in Three Phase AC


Circuit using
I = P/ (√3 x VL x Cos Ф)

Print Electrical Current in Three


Phase AC Circuit (I)

Stop

Output:

Enter the power value : 100

Enter the Voltage value : 8

Electrical Current in Three Phase AC Circuit : 7.21

Result:

Thus an algorithm to compute electrical current in three phase AC circuitwas written


and the flowchart was developed successfully.
Ex. No. 2(a) Simple Statements & Expressions - Exchange the Values
ofTwoVariables

Date:

Aim:

To implement a python program to exchange the values of two variables using simple
statements and expressions

Algorithm:

Step 1: Start the Program

Step 2: Get two integer inputs a and b from the user using the input() function.

Step 3: Swap the values of variables directly using the formula (a, b) = (b, a)

Step 4: Print the result

Step 5: Stop the program

Flowchart:
Program:

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

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

print("Before swapping")

print(a,b)

(a,b)=(b,a)

print("After swapping")

print(a,b)

Output:

Result:

Thus the Python program to exchange the values of two variables using simple
statements and expressions was written, executed and the output was verified successfully.
Ex. No. 2(b) Circulate the Values of n Variables

Date:

Aim:

To implement a python program to circulate the values of n variables using simple


statements and expressions

Algorithm:

Step 1: Start the Program

Step 2: Get the input value of total number of terms

Step 3: Get the integer values to circulate

Step 4: Append the values in a list

Step 5: for val in range(0,no_of_terms,1)

A) Pop the first value and append to the last in the list

B) Print the result

Step 6: Repeat the step 5 until all the values are circulated

Step 7: Stop the program

Flowchart:
Program:

no_of_terms = int(input("Enter number of values : "))

#Read values

list1 = []

forval in range(0,no_of_terms,1):

ele = int(input("Enter integer : "))

list1.append(ele)

#Circulate and display values

print("Circulating the elements of list ", list1)

forval in range(0,no_of_terms,1):

ele = list1.pop(0)

list1.append(ele)

print(list1)

Output:
Result:

Thus the Python program to circulate the values of n variables using simple
statements and expressionswas written, executed and the output was verified successfully.
Ex. No. 2(c) Distance between Two Points

Date:

Aim:

To implement a python program to find the distance between two points using simple
statements and expressions

Algorithm:

Step 1: Start the program

Step 2: Read the coordinates of 1 st and 2nd point

Step 3: Compute the distance using the formula √(𝑥2 − 𝑥1)2 + (𝑦2 − 𝑦1)2

Step 4: Print Distance

Step 5: Stop the program

Flowchart:
Program:

x1=int(input("enter x1 : "))

y1=int(input("enter y1 : "))

x2=int(input("enter x2 : "))

y2=int(input("enter y2 : "))

distance = ((((x1 - x2 ) + (x1 - x2)) + ((y1 - y2) + (y1 - y2)))**0.5)

print("distance between",(x1,y1),"and",(x2,y2),"is : ",distance)

Output:
Result:

Thus the Python program to find the distance between two points using simple
statements and expressions was written, executed and the output was verified successfully.
Ex. No. 3(a) Conditionals and Iterative Loops –NumberSeries –
Fibonacci Series

Date:

Aim:

To implement a python program to find the Fibonacci Series of n terms using simple
conditionals and iterative loops

Algorithm:

Step 1: Start the program

Step 2: Get the input value from the user to calculate the Fibonacci series

Step 3: Initialize n1 = 0 & n2

Step 4: Check

A) ifnterms<= 0, then Print the value 0

B) elifnterms == 1, then Print the value 1

C) else use the formula,

xn = xn-1 + xn-2 ; where


xn is term number “n”
xn-1 is the previous term (n-1)
xn-2 is the term before that

Step 5: Print the result

Step 6: Stop the program


Flowchart:

Program:

# Program to display the Fibonacci sequence up to n-th term

nterms = int(input("Fiboacci series of: "))

# first two terms

n1, n2 = 0, 1

count = 0

# check if the number of terms is valid

ifnterms<= 0:

print("Please enter a positive integer")


# if there is only one term, return n1

elifnterms == 1:

print("Fibonacci sequence upto",nterms,":")

print(n1)

# generate fibonacci sequence

else:

print("Fibonacci sequence:")

while count <= nterms:

print(n1)

nth = n1 + n2

# update values

n1 = n2

n2 = nth

count += 1

Output:
Result:

Thus the Python program to find the Fibonacci Series of n terms using simple
conditionals and iterative loops was written, executed and the output was verified
successfully.
Ex. No. 3(b) Number Pattern – Simple Pattern

Date:

Aim:

To implement a python program to find the Number Pattern using simple conditionals
and iterative loops

Algorithm:

Step 1: Start the program

Step 2: Define the depth value

Step 3: Outer loop will print number of rows

Step 4: Inner loop will print the value of I after each iteration

Step 5: Print number

Step 6: Line after each row to display pattern correctly

Step 7: Stop the program


Flowchart:
Program:

# Number Pattern

depth = 6

for number in range(depth):

fori in range(number):

print (number,end = " ")

print (" ")

Output:

Result:

Thus the Python program to find the Number Pattern using simple conditionals and
iterative loops was written, executed and the output was verified successfully.
Ex. No. 3(c) Number Pattern – Pascal’s Triangle

Date:

Aim:

To implement a python program to find the Number Pattern for Pascal’s Triangle
using simple conditionals and iterative loops

Algorithm:

Step 1: Start the program

Step 2: Taking input from the user to get the number of rows.

Step 3: Declare an empty list that will store the values.

Step 4: Using for loop, which will iterate through 0 to n - 1, append the sub-lists to the
list.

Step 5: Now append 1 to the list.

Step 6: Now, use for loop to define the value of the number inside the triangle
adjacent row.

Step 7: Print Pascal triangle according to the format.

Step 8: Stop the program


Flowchart:
Program:

# Pascal Triangle

num = int(input("Enter the number: "))

list1 = [] #an empty list

fori in range(num):

list1.append([])

list1[i].append(1)

for j in range(1, i):

list1[i].append(list1[i - 1][j - 1] + list1[i - 1][j])

if(num != 0):

list1[i].append(1)

fori in range(num):

print(" " * (num - i), end = " ", sep = " ")

for j in range(0, i + 1):

print('{0:6}'.format(list1[i][j]), end = " ", sep = " ")

print()

Output:
Result:

Thus the Python program to find the Number Pattern for Pascal’s Triangle using
simple conditionals and iterative loops was written, executed and the output was verified
successfully.
Ex. No. 3(d) Pyramid Pattern – Triangle Pattern

Date:

Aim:

To implement a python program to print the triangle pyramid pattern using simple
conditionals and iterative loops

Algorithm:

Step 1: Start the program

Step 2: Initialize the variable n to enter the number of rows for the pattern

Step 3: Compute m = (2 * n) – 2

Step 4: Decrement the value of m after each loop

Step 5: Print the Triangle pyramid using stars

Step 6: Stop the program

Flowchart:
Start
Program:

# Printing pattern triangle

n = int(input("Enter the number of rows: "))

m = (2 * n) - 2

fori in range(0, n):

for j in range(0, m):

print(end=" ")

m = m - 1 # decrementing m after each loop

for j in range(0, i + 1):

# printing full Triangle pyramid using stars

print("* ", end=' ')

print(" ")

Output:

Result:

Thus the Python program to print the triangle pyramid pattern using simple
conditionals and iterative loops was written, executed and the output was verified
successfully.
Ex. No. 4(a) Operations of List - Items Present in a Library,
Components of a Car, Materials Required for Construction of a Building

Date:

Aim:

To implement a python program to do the operations of list for the items present in a
library, components of a car and materials required for construction of a building

Algorithm:

Step 1: Start the program

Step 2: Get the library items in the form of list

Step 3: Get the car components in the form of list

Step 4: Get the materials required for construction of a building in the form of list

Step 5: Perform the list operations on that list

Step 6: Print the result

Step 7: Stop the program

Program:

#Operations using List

print ("Using List")

liblist1 = ['books', 'periodicals', 'newspapers', 'manuscripts', 'films', 'maps', 'prints',


'documents', 'microform', 'CDs', 'cassettes', 'videotapes', 'DVDs', 'Blu-ray Discs', 'e-
books', 'audiobooks', 'video games']

carlist2 = ["engine","battery", "transmission", "steering", "brakes", "videoplayers",


"sensors"]

constructionlist3 = ["cement", "steel", "concrete", "wires", "sand", "bricks"]

item = input("Enter what you need from library:")


if item in liblist1:

print ("Item you needed from library is available. Visit library to collect it")

else:

print("Sorry, Not available")

item = input("Enter the components you looking for in a car:")

if item in carlist2:

print ("Component you looking in a car is available")

else:

print("Sorry, Not available")

item = input("Enter your required component for a construction:")

if item in constructionlist3:

print ("Your required component is available. Contact us for more details")

else:

print("Sorry, Not available")

print(liblist1 + carlist2) # List concatenation

print (carlist2 * 2) # List Repetition

print (constructionlist3[1:5]) # List Slicing

liblist1.append('globe') # List append

print (liblist1)

print ('Count for newspapers:', liblist1.count('newspapers')) # List count presence of


the element

liblist1.extend(['e-books'])

print ('After extend:', liblist1) # List extend


print ('Index of films:', liblist1.index('films')) # List index value is printed for the
element

carlist2.insert(1,'mirror') # List insert the element at index position 1

print ('After insert:',carlist2)

print (constructionlist3.pop (5)) # List pop the indexed element

print (constructionlist3)

liblist1.remove('e-books') # List remove the element

print (liblist1)

carlist2.reverse() # List reversing the elements

print (carlist2)

constructionlist3.sort() # List sorting the elements in ascending order

print ('After sorting:', constructionlist3)

liblist1[2] = 'magazines' #List Mutation (Changing the element)

print ('After mutation:', liblist1)

print (liblist1 is carlist2) #list Aliasing (Identity operation)

liblist2 = list(liblist1) # List Cloning built -in function

print ('New list:', liblist2)

import copy

liblist2 = copy.copy(liblist1) # List Cloning copy function

print ('New list:', liblist2)


Output:
Result:

Thus the Python program to implement the operations of list for the items present in a
library, components of a car and materials required for construction of a buildingwas written,
executed and the output was verified successfully.
Ex. No. 4(b) Operations of Tuple - Items Present in a Library,
Components of a Car, Materials Required for Construction of a Building

Date:

Aim:

To implement a python program to do the operations of tuple for the items present in
a library, components of a car and materials required for construction of a building

Algorithm:

Step 1: Start the program

Step 2: Get the library items in the form of tuple

Step 3: Get the car components in the form of tuple

Step 4: Get the materials required for construction of a building in the form of tuple

Step 5: Perform the list operations on that tuple

Step 6: Print the result

Step 7: Stop the program

Program:

#Operations using Tuple

print ("Using Tuple")

libtuple1 = ('books', 'periodicals', 'newspapers', 'manuscripts', 'films', 'maps', 'prints',


'documents', 'microform', 'CDs', 'cassettes', 'videotapes', 'DVDs', 'Blu-ray Discs', 'e-
books', 'audiobooks', 'video games')

cartuple2 = ("engine","battery", "transmission", "steering", "brakes", "videoplayers",


"sensors")

constructiontuple3 = ("cement", "steel", "concrete", "wires", "sand", "bricks")

item = input("Enter what you need from library:")


if item in libtuple1:

print ("Item you needed from library is available. Visit library to collect it")

else:

print("Sorry, Not available")

item = input("Enter the components you looking for in a car:")

if item in cartuple2:

print ("Component you looking in a car is available")

else:

print("Sorry, Not available")

item = input("Enter your required component for a construction:")

if item in constructiontuple3:

print ("Your required component is available. Contact us for more details")

else:

print("Sorry, Not available")

print(libtuple1 + cartuple2) # Tuple concatenation

print (cartuple2 * 2) # Tuple Repetition

print (constructiontuple3[1:3]) # Tuple Slicing

print ('Count for cement:', constructiontuple3.count('cement')) # Tuple count presence


of the element

print ('Index of steering:', cartuple2.index('steering')) # Tuple index value is printed


for the element

print ('sand' in constructiontuple3) #Tuple Membership operation

print (len(cartuple2)) # Tuple Length

print (sorted(constructiontuple3)) #Tuple Sorting


Output:
Result:

Thus the Python program to implement the operations of tuple for the items present in
a library, components of a car and materials required for construction of a building was
written, executed and the output was verified successfully.
Ex. No. 5(a) Operations of Sets – Language, Components of an
automobile, Elements of a Civil Structure

Date:

Aim:

To implement a python program to do the operations of sets for languages,


components of an automobile and elements of a civil structure

Algorithm:

Step 1: Start the program

Step 2: Get the languages as input in the form of set

Step 3: Get the components of an automobile as input in the form of set

Step 4: Get the elements of a civil structure as input in the form of set

Step 5: Perform the list operations on that set

Step 6: Print the result

Step 7: Stop the program

Program:

#Operations using Sets

print ("Using Sets")

automobile = {"engine", "battery", "transmission", "steering","brakes", "video


players", "sensors"}

construction = {"cement", "steel", "concrete", "wires", "sand", "bricks"}

languages = {"c", "c++", "c#", "java", "javascript", "php", "python", "ruby" "pearl",
"html"}

keyword = input("Enter the parts of an automobile you are looking for:")

if keyword in automobile:
print ("Component you looking in a car is available")

else:

print("Sorry, Not available")

keyword = input("Enter the components you are looking for construction:")

if keyword in construction:

print ("Component you are looking for construction is available")

else:

print("Sorry, Not available")

keyword = input("Enter the language you are looking for:")

if keyword in languages:

print ("Language you are looking for programming is available")

else:

print("Sorry, Not available")

construction.update({"water","msand","psand"}) # Add multiple elements in the Set

print(construction)# Display Values

languages.discard("c#") # Removes an element in the Set

print(languages)

print(automobile | construction) # Set Union

print(construction & languages) # Set Intersection

print(automobile - languages) # Set Difference

print(automobile ^ languages) # Set Symmetric Difference

print("engine" in automobile) # Set Membership Test Operation

print(len(construction)) # Length of a Set


Output:
Result:

Thus the Python program to implement the operations of sets for languages,
components of an automobile and elements of a civil structurewas written, executed and the
output was verified successfully.
Ex. No. 5(b) Operations of Dictionaries – Language, Components of
an automobile, Elements of a Civil Structure

Date:

Aim:

To implement a python program to do the operations of dictionaries for languages,


components of an automobile and elements of a civil structure

Algorithm:

Step 1: Start the program

Step 2: Get the languages as input in the form of dictionary

Step 3: Get the components of an automobile as input in the form of dictionary

Step 4: Get the elements of a civil structure as input in the form of dictionary

Step 5: Perform the list operations on that dictionary

Step 6: Print the result

Step 7: Stop the program

Program:

#Operations using Dictionaries

print ("Using Dictionaries")

automobile = {'1':"engine", '2':"battery", '3':"transmission", '4':"steering", '5':"brakes",


'6':"video players", '7':"sensors"}

construction = {'1':"cement", '2':"steel", '3':"concrete", '4':"wires", '5':"sand",


'6':"bricks"}

languages = {'2':"c", '1':"c++", '3':"c#", '4':"java", '5':"javascript", '6':"php",


'7':"python", '8':"ruby", '9':"pearl"}

keyword = input("Enter the parts of an automobile you are looking for:")


if keyword in automobile:

print ("Component you looking in a car is available")

else:

print("Sorry, Not available")

keyword = input("Enter the components you are looking for construction:")

if keyword in construction:

print ("Component you are looking for construction is available")

else:

print("Sorry, Not available")

keyword = input("Enter the language you are looking for:")

if keyword in languages:

print ("Language you are looking for programming is available")

else:

print("Sorry, Not available")

print(len(construction))# Returns No. of Pairs on a Dictionary

print(sorted(languages)) # Returns Sorted List of Keys in Dictionary

print (automobile.keys()) # Return keys

Output:
Result:

Thus the Python program to implement the operations of dictionaries for languages,
components of an automobile and elements of a civil structure was written, executed and the
output was verified successfully.
Ex. No. 6(a) Finding the Factorial of a Number – Using Functions

Date:

Aim:

To implement a python program to find the factorial of a given number using


functions

Algorithm:

Main function:

Step 1: Start the program

Step 2: Declare num

Step 3: Read num

Step 4: if (num&gt;0):

Step 4.1: Display “Factorial does not exist for negative numbers” go to step 5

Step 5: Elif (num == 0):

Step 5.1: Display &quot; The factorial of 0 is 1&quot; go to step 6

Step 6: Else

Step 6.1: Call Function recur_factorial (num)

Step 6.2: Display “The factorial of the number”

Step 7: Stop the program

Sub Function:

Step 1: Call recur_factorial(num)

Step 2: Declare n

Step 3: if (n == 1)
Step 3.1: Return the value of n

Step 4: else

Step 4.1: n * recur_factorial(num)

Step 5: Return

Flowchart:

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("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 Python program to find the factorial of a given number using functions was
written, executed and the output was verified successfully.
Ex. No. 6(b) Finding the largest Number in a List – Using Functions

Date:

Aim:

To implement a python program to find the largest in a list using functions

Algorithm:

Main function:

Step 1: Start the program

Step 2: Declare num, list

Step 3: Read num

Step 3.1: Display “Please enter the Total Number of List Elements”

Step 4: for i in range (1, num+1)

Step 4.1: Declare num, i=1

Step 4.2: Read value, num

Step 4.2.1: Display “Please enter the Value of i Element:”

Step 4.3: Append the values into the list=[]

Step 4.4: Increment num=num+1

Step 5: Calling function max_num_in_list( list )

Step 6: Stop the program

Sub function:

Step 1: Call max_num_in_list( list )

Step 2: Declare max,a

Step 3: Read max value

Step 4: for a in list


Step 4.1: if (a>max):

Step 4.1.1: Declare max=a

Step 5: Display max

Step 6: Return

Flowchart:

Start

def
max_num_in_list(list)

Program:

list = []

num = int(input("Enter number of elements in list:"))

for i in range(1, num + 1):

value = int(input("Please Enter the Value of %d Element : " %i))

list.append(value)

print(list)

def max_num_in_list(list):

max = list[0]
for a in list:

if a > max:

max = a

return max

print(max_num_in_list(list))

Output:

Result:

Thus the Python program to find the largest number in a list using functions was
written, executed and the output was verified successfully.
Ex. No. 6(c) Finding the Area of Shapes – Using Functions

Date:

Aim:

To implement a python program to find the area of shapes using functions

Algorithm:

Step 1: Start the program

Step 2: Declare a constant PI = 3.14

Step 3: Take a numeric value (radius) from the user

Step 4: Use the formulas to find the area of square, rectangle, circle, triangle and
parallelogram

Step 5: Display the result

Step 6: Stop the program

Flowchart:
Program:

# define a function for calculating

# the area of a shapes

def calculate_area(name):

# converting all characters

# into lower cases

name = name.lower()

# check for the conditions

if name == "rectangle":

l = int(input("Enter rectangle's length: "))

b = int(input("Enter rectangle's breadth: "))

# calculate area of rectangle

rect_area = l * b

print(f"The area of rectangle is {rect_area}.")

elif name == "square":

s = int(input("Enter square's side length: "))

# calculate area of square

sqt_area = s * s
print(f"The area of square is {sqt_area}.")

elif name == "triangle":

h = int(input("Enter triangle's height length: "))

b = int(input("Enter triangle's breadth length: "))

# calculate area of triangle

tri_area = 0.5 * b * h

print(f"The area of triangle is {tri_area}.")

elif name == "circle":

r = int(input("Enter circle's radius length: "))

pi = 3.14

# calculate area of circle

circ_area = pi * r * r

print(f"The area of circle is {circ_area}.")

elif name == 'parallelogram':

b = int(input("Enter parallelogram's base length: "))

h = int(input("Enter parallelogram's height length: "))

# calculate area of parallelogram


para_area = b * h

print(f"The area of parallelogram is {para_area}.")

else:

print("Sorry! This shape is not available")

# driver code

if __name__ == "__main__" :

print("Calculate Shape Area")

shape_name = input("Enter the name of shape whose area you want to find: ")

# function calling

calculate_area(shape_name)

Output:
Result:

Thus the Python program to find the area of shapes using functions was written,
executed and the output was verified successfully.
Ex. No. 7(a) Finding the Reverse of the Elements of a String

Date:

Aim:

To implement a python program to find the reverse of the elements of a string

Algorithm:

Step 1: Start the program

Step 2: Read the string from the user

Step 3: Calculate the length of the string

Step 4: Initialize rev = “ ” [empty string]

Step 5: Initialize i = length - 1

Step 6: Repeat until i>=0:

Step 6.1: rev = rev + Character at position 'i' of the string

Step 6.2: i = i – 1

Step 7: Print rev

Step 9: Stop the program


Flowchart:

Program:

def reverse_string(str2):

str1 = "" # Declaring empty string to store the reversed string


for i in str2:

str1 = i + str1

return str1 # It will return the reverse string to the caller function

str_new = "JavaTpoint" # Given String

print("The original string is: ",str_new)

print("The reverse string is",reverse_string(str_new)) # Function call

Output:

Result:

Thus the Python program to find the reverse of the elements of a string was written,
executed and the output was verified successfully.

You might also like