[go: up one dir, main page]

0% found this document useful (0 votes)
14 views36 pages

PSPP Lab Manual Programs 1 to 7.

Pps

Uploaded by

poketype009
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)
14 views36 pages

PSPP Lab Manual Programs 1 to 7.

Pps

Uploaded by

poketype009
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/ 36

Ex. No: 1.

A)ELECTRICITY BILLING
Aim
To draw flowchart for Electricity Billing
Algorithm:
1. Start
2. Read the number of units consumed (as units).
3. Initialize a variable amount to store the total billing amount.
4. Check if units is less than or equal to 100:
o Set amount = 0 (no charge for the first 100 units).
5. Else if units is greater than 100 but less than or equal to 200:
o Calculate amount = (units - 100) * 1.5 (charge 1.5 per unit for the units above 100).
6. Else if units is greater than 200 but less than or equal to 500:
o Calculate amount = (100 * 0) + (100 * 1.5) + (units - 200) * 3 (charge 1.5 per unit for 101 to 200
units and 3 per unit for units above 200).
7. Else (if units is greater than 500):
o Calculate amount = (100 * 0) + (100 * 1.5) + (300 * 3) + (units - 500) * 6.6 (charge 1.5 per unit for
101 to 200 units, 3 per unit for 201 to 500 units, and 6.6 per unit for units above 500).
8. Print the calculated amount.
9. End
Program:
def calculate_amount(units):
if units <= 100:
amount = 100 * 0
elif 100 < units <= 200:
amount = (100 * 0) + (units - 100) * 1.5
elif 200 < units <= 500:
amount = (100 * 0) + (200 - 100) * 2 + (units - 200) * 3
else: # units > 500
amount = (100 * 0) + (200 - 100) * 2 + (500 - 200) * 3 + (units - 500) * 6.6
return amount
units = float(input("Enter the number of units: "))
amount = calculate_amount(units)
print(f"The calculated amount is: {amount}")
Flowchart

Start

Read Units

<=100
<=200<=500 Units= >500
?

Amount= Amount = (100* 0) Amount = (100* 0)


Amount=100*0 (100*0)+(Unit– +(200–100)*2+ +(200 – 100)*3.5
100)*1.5 (Unit–200)*3 +(500 –200)*4.6+
(Unit –500)*6.6

Print Amount

Stop

Result
Thus the flowchart for Electricity Billing was drawn successfully.
Ex. No: 1.B) RETAIL SHOPBILLING

Aim
To draw flowchart for Retail shop billing.

Algorithm:
1. Start
2. Initialize a dictionary rate_of_item containing items and their corresponding rates (e.g., {"item1": 50,
"item2": 30, "item3": 20}).
3. Print the available items and their rates.
4. Initialize an empty dictionary quantity to store the quantity for each item.
5. For each item in rate_of_item:
o Prompt the user to input the quantity of that item.
o Store the quantity in the quantities dictionary.
6. Initialize a variable cost = 0 to store the total cost.
7. For each item in quantities:
o Multiply the quantity by the corresponding rate from rate_of_item.
o Add this value to cost.
8. Calculate the total bill by adding tax (18%) to the cost:
o bill_amount = cost + (cost * 0.18)
9. Print the total bill amount including tax.
10. End

Program:
tax = 0.18
rate_of_item = {
"item1": 50,
"item2": 30,
"item3": 20
}

print("Available items and their rates:")


for item, rate in rate_of_item.items():
print(f"{item}: {rate} per unit")
# Get the quantity of each item from the user
quantities = {}
for item in rate_of_item.keys():
quantities[item] = int(input(f"Enter quantity for {item}: "))

# Calculate the total cost


cost = 0
for item, quantity in quantities.items():
cost += rate_of_item[item] * quantity

# Add tax to the total cost


bill_amount = cost + (cost * tax)

# Print the total bill amount including tax


print(f"\nThe total bill amount including tax is: {bill_amount}")
Flowchart

Start

tax=0.18
Rate_of_item

Display Items

Read Quantities

Cost=Rate_of_item *
quantity+Rate_of_item
*quantity+……………..

Bill_Amount=cost+cost*tax

Print Bill_Amount

Stop

Result
Thus the flowchart for Retail shop billing was drawn successfully
Ex. No: 1.C) SINESERIES

Aim
To evaluate and draw flowchart for Sine Series.
Algorithm:
1. Start
2. Read the value of x (in degrees).
3. Read the number of terms n.
4. Convert x from degrees to radians.
5. Initialize t and sum_sin to the value of x_radians.
6. For each term from 1 to n-1:
o Update the term t using the formula for the next term in the Taylor series.
o Add the updated term t to sum_sin.
7. Print the calculated sine value (sum_sin).
8. Print the sine value using the math.sin() function.
9. End.
Program:
import math
# Input the angle in degrees and the number of terms
x = float(input("Enter the value of x (in degrees): "))
n = int(input("Enter the number of terms: "))
# Convert degrees to radians
x_radians = x * 3.14159 / 180
# Initialize the first term and sum
t = x_radians
sum_sin = x_radians
# Compute the sine using the Taylor series
for i in range(1, n):
t = (-t * x_radians * x_radians) / (2 * i * (2 * i + 1))
sum_sin += t
# Print the results
print(f"The calculated sin({x}) is: {sum_sin}")
print(f"The value of sin({x}) using the math module is: {math.sin(x_radians)}")
Flowchart
Start

Read x, n

x=x*3.14159/180
t=x
sum=x

i=1; i<n; i++

t=(t*(-
1)*x*x)/(2*i*(2*i+1));sum=su

Print sum as sin(x)

Stop

Result
Thus the flowchart for Sine Series was drawn successfully
Ex. No: 1.D) WEIGHTOF AMOTORBIKE

Aim
To draw flowchart for weight of Motorbike.
Algorithm:
1. Start
2. Read the weight of the vehicle (in kg) and store it in the variable weight.
3. Check the value of weight:
o If weight == 315, print "Chopper".
o Else if weight == 180, print "Sports Bike".
o Else if weight == 250, print "Cruiser".
o Else if weight == 110, print "Scooter".
o Else if weight == 340, print "Touring Bike".
o Else if weight == 400, print "Bagger".
o Else, print "No matching vehicle type found."
4. End.
Program:

weight = int(input("Enter the weight of the vehicle (in kg): "))


if weight == 315:
print("Chopper")
elif weight == 180:
print("Sports Bike")
elif weight == 250:
print("Cruiser")
elif weight == 110:
print("Scooter")
elif weight == 340:
print("Touring Bike")
elif weight == 400:
print("Bagger")
else:
print("No matching vehicle type found.")
Flowchart
Start

Read weight

==315 kg ==180 kg ==250 kg ==115 kgweig=h=t4


=0? 0 kg==340 kg

Print Print Print Print Print Print


Chopper Sports Cruiser Scooter Touring Bagger
Bike Bike

Stop

Result
Thus the flowchart for weight of Motorbike was drawn successfully.
Ex. No: 1.E) WEIGHTOFA STEELBAR

Aim
To draw flowchart for Weight of a Steel Bar.
Algorithm:
1. Start
2. Read the value of the diameter.
3. Calculate W=D2/162
4. Print the calculated value of W.
5. End
Program:
D = float(input("Enter the diameter (D): "))
W = (D ** 2) / 162
print(f"The calculated value of W is: {W:.4f}") # Rounded to 4 decimal places for clarity
Flowchart

Start

Read Diameter D

W=D2/162

Print W

Stop

Result
Thus the flowchart for Weight of a Steel Bar was drawn successfully.
Ex. No: 1.F) COMPUTE ELECTRICAL CURRENT IN THREEPHASE ACCIRCUIT

Aim
To draw flowchart for Compute electrical current in three phase AC circuit.

Algorithm:
1. Start
2. Read the line voltage and current Vline, Aline.
3. Calculate the apparent power A=√𝟑 *Vline * Aline
4. Print the calculated apparent power VA.
5. End

Program:
import math

# Input line voltage and current


Vline = float(input("Enter the line voltage (Vline): "))
Aline = float(input("Enter the line current (Aline): "))

# Calculate the apparent power


VA = math.sqrt(3) * Vline * Aline

# Print the apparent power with 2 decimal places


print(f"The apparent power (VA) is: {VA:.2f} VA")
Flowchart

Start

Read Vline, Aline

VA=√𝟑 *Vline * Aline

Print VA

Stop

Result
Thus the flowchart for Compute electrical current in three phase AC circuit was drawn successfully
Ex. No: 2.A) EXCHANGE THE VALUES OF TWO VARIABLES USING TEMPORARY VARIABLE
Aim
To write a program to exchange the values of two variables using third variable

Algorithm
1. Start
2. Read x and y
3. Print x and y value before swap
3. Swap values using temporary variable
temp=x
x=y
y=temp
4. Print x and y value after swap
5. Stop

Program
x = int(input("Enter x value:"))
y = int(input("Enter y value:"))
print("Value of x and y before swap:",x,y)
temp = x
x=y
y = temp
print("Value of x and y after swap:",x,y)

Output
Enter x value:10
Enter y value:23
Value of x and y before swap: 10 23
Value of x and y after swap: 23 10

Result
Thus the Python program to exchange the values of two variables by using a third variable
was executed successfully
Ex. No: 2.B) EXCHANGE THE VALUES OF TWO VARIABLES WITHOUT USING TEMPORARY
VARIABLE
Aim
To write a program to exchange the values of two variables without using third variable

Algorithm
1. Start
2. Read x and y
3. Print x and y value before swap
3. Swap values without temporary variable
x=x+y
y=x-y
x=x-y
4. Print x and y value after swap
5. Stop

Program
x = int(input("Enter x value:"))
y = int(input("Enter y value:"))
print("Value of x and y before swap:",x,y)
x=x+y
y=x-y
x=x-y
print("Value of x and y after swap:",x,y)

Output
Enter x value:24
Enter y value:45
Value of x and y before swap: 24 45
Value of x and y after swap: 45 24

Result
Thus the Python program to exchange the values of two variables without using a third variable
was executed successfully.
Ex. No: 2.C) CIRCULATE THE VALUES OF N VARIABLES
Aim
To write a program to circulate the values of N variables.

Algorithm
1. Start
2. Read upper limit n
3. Read n element using loop
4. Store elements in list
5. POP out each element from list and append to list
6. Print list
7. Stop

Program
n = int(input("Enter number of values:"))
list1=[]
fori in range(0,n,1):
x=int(input("Enter integer:"))
list1.append(x)
print("Circulating the elements of list:",list1)
fori in range(0,n,1):
x=list1.pop(0)
list1.append(x)
print(list1)
Output
Enter number of values:3
Enter integer:2
Enter integer:3
Enter integer:5
Circulating the elements of list: [2, 3, 5]
[3, 5, 2]
[5, 2, 3]
[2, 3, 5]

Result
Thus the Python program to circulate the values of N variables was executed successfully.
Ex. No: 2.D) DISTANCE BETWEEN TWO VARIABLES

Aim
To write a program to find distance between two variables.

Algorithm
1. Start
2. Read four coordinates x1, y1, x2, y2

3. Find distance using


4. Print distance
5. Stop

Program
x1=int(input("Enter x1 value: "))
y1=int(input("Enter y1 value: "))
x2=int(input("Enter x2 value: "))
y2=int(input("Enter y2 value: "))
distance=((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))**0.5
print("Distance = ",distance)

Output
Enter x1 value: 1
Enter y1 value: 2
Enter x2 value: 3
Enter y2 value: 4
Distance = 2.8284271247461903

Result
Thus the Python program to find distance between two variables was executed successfully.
Ex. No: 3.A) NUMBER SERIES

Aim
To write a program to evaluate number series 12+22+32+….+N2

Algorithm
1. Start
2. Read maximum limit n
3. Initialize sum=0 and i=1
4. Calculate sum of series
while i<=n:
sum=sum+i*i
incrementi
5. Print sum
6. Stop

Program
n = int(input('Enter a number: '))
sum=0
i=1
while i<=n:
sum=sum+i*i
i+=1
print('Sum = ',sum)

Output
Enter a number: 3
Sum = 14

Result
Thus the Python program to evaluate number series was executed successfully.
Ex. No: 3.B) NUMBER PATTERN

Aim
To write a program to print number pattern

Algorithm
1. Start
2. Read upper limit N
3. Print pattern using nested for loop
For i in range(1,N+1)
for k in range (N,i-1)
print empty space
for j in range(1,i+1)
print j
for l in range(i-1,0,-1)
print l
4. Stop

Program
rows = int(input("Enter the number of rows: "))
for i in range(rows+1):
for j in range(i):
print(i, end=" ")
print("")

Output
Enter the number of rows: 5
1
22
333
4444
55555
Result
Thus the Python program to print number pattern was executed successfully.
Ex. No: 4.A) ITEMS PRESENT IN A LIBRARY

Aim
To write a program to implement operations in a library list

Algorithm
1. Start
2. Read library list
3. Print library details
4. Print first and fourth position element
5. Print items from index 0 to 4
6. Append new item in library list
7. Print index of particular element
8. Sort and print the element in alphabetical order
9. Pop the element
10. Remove the element
11. Insert an element in position
12. Count the number of elements in library
13. Stop

Program
library =['Books','Periodicals','Newspaper','Manuscripts','Maps','Prints','Documents','Ebooks']
print('Library: ',library)
print('First element: ',library[0])
print('Fourth element: ',library[3])
print('Items in Library from 0 to 4 index: ',library[0: 5])
print('-7th element: ',library[-7])
library.append('Audiobooks')
print('Library list after append( ): ',library)
print('Index of \'Newspaper\': ',library.index('Newspaper'))
library.sort( )
print('After sorting: ', library);
print('Popped elements is: ',library.pop( ))
print('After pop( ): ', library);
library.remove('Maps')
print('After removing \'Maps\': ',library)
library.insert(2, 'CDs')
print('After insert: ', library)
print('Number of Elements in Library list: ',library.count('Ebooks'))

Output
Library: ['Books', 'Periodicals', 'Newspaper', 'Manuscripts', 'Maps', 'Prints', 'Documents', 'Ebooks']
First element: Books
Fourth element: Manuscripts
Items in Library from 0 to 4 index: ['Books', 'Periodicals', 'Newspaper', 'Manuscripts', 'Maps']
-7th element: Periodicals
Library list after append( ): ['Books', 'Periodicals', 'Newspaper', 'Manuscripts', 'Maps', 'Prints', 'Documents',
'Ebooks', 'Audiobooks']
Index of 'Newspaper': 2
After sorting: ['Audiobooks', 'Books', 'Documents', 'Ebooks', 'Manuscripts', 'Maps', 'Newspaper',
'Periodicals', 'Prints']
Popped elements is: Prints
After pop( ): ['Audiobooks', 'Books', 'Documents', 'Ebooks', 'Manuscripts', 'Maps', 'Newspaper', 'Periodicals']
After removing 'Maps': ['Audiobooks', 'Books', 'Documents', 'Ebooks', 'Manuscripts', 'Newspaper',
'Periodicals']
After insert: ['Audiobooks', 'Books', 'CDs', 'Documents', 'Ebooks', 'Manuscripts', 'Newspaper', 'Periodicals']
Number of Elements in Library list: 1

Result
Thus the Python program toprint items present in a library using list operationwas executed successfully.
Ex. No: 4.B) COMPONENTS OF A CAR

Aim
To write a program to implement operations of Tuple using Components of a Car

Algorithm
1. Start
2. Read car tuple
3. Print components of car
4. Print first and fourth position element
5. Print items from index 0 to 4
6. Print index of particular element
7. Count the number of ‘Seat Belt element’ in car tuple
8. Count number of elements in car tuple
9. Stop

Program
car = ('Engine','Battery','Alternator','Radiator','Steering','Break','SeatBelt')
print('Components of a car: ',car)
print('First element: ',car[0])
print('Fourth element: ',car[3])
print('Components of a car from 0 to 4 index: ',car[0: 5])
print('3rd or -7th element: ',car[-7])
print('Index of \'Alternator\': ',car.index('Alternator'))
print('Number of Elements in Car Tuple : ',car.count('Seat Belt'))
print('Length of Elements in Car Tuple : ',len(car))
Output
Components of a car: ('Engine', 'Battery', 'Alternator', 'Radiator', 'Steering', 'Break', 'SeatBelt')
First element: Engine
Fourth element: Radiator
Components of a car from 0 to 4 index: ('Engine', 'Battery', 'Alternator', 'Radiator', 'Steering')
3rd or -7th element: Engine
Index of 'Alternator': 2
Number of Elements in Car Tuple: 0
Length of Elements in Car Tuple: 7

Result
Thus the Python program to print components of a car using tuple operation was executed successfully.
Ex. No: 4.C) MATERIALS REQUIRED FOR CONSTRUCTION OF A BUILDING

Aim
Towrite a program for operations of list to printmaterials required for construction of a building

Algorithm
1. Start
2. Read material list
3. Print material details
4. Print first and fourth position element
5. Print number of elements in material list
6. Print items from index 0 to 2
7. Append new item in material list
8. Print list of material after append
9. Print ending alphabet of material
10. Print starting alphabet of material
11. Print index of particular element
12. Sort and print the element in alphabetical order
13. Pop the element and print remaining element of material
14. Remove an element and print remaining element of material
15. Insert an element in position
16. Print the element after inserting
17. Stop

Program
material =['Wood','Concrete','Brick','Glass','Ceramics','Steel']
print('Building Materials',material)
print('First element: ',material[0])
print('Fourth element: ',material[3])
print('Number of Elements in Material list : ',len(material))
print('Items in Material from 0 to 2 index: ',material[0: 3])
print('-3th element: ',material[-3])
material.append('Water')
print('Material list after append(): ',material)
print('Ending Alphabet of Material',max(material))
print('Starting Alphabet of Material',min(material))
print('Index of Brick: ',material.index('Brick'))
material.sort( )
print('After sorting: ', material);
print('Popped elements is: ',material.pop())
print('After pop(): ', material);
material.remove('Glass')
print('After removing Glass: ',material)
material.insert(3, 'Stone')
print('After insert: ', material)

Output
Building Materials ['Wood', 'Concrete', 'Brick', 'Glass', 'Ceramics', 'Steel']
First element: Wood
Fourth element: Glass
Number of Elements in Material list : 6
Items in Material from 0 to 2 index: ['Wood', 'Concrete', 'Brick']
-3th element: Glass
Material list after append(): ['Wood', 'Concrete', 'Brick', 'Glass', 'Ceramics', 'Steel', 'Water']
Ending Alphabet of Material Wood
Starting Alphabet of Material Brick
Index of Brick: 2
After sorting: ['Brick', 'Ceramics', 'Concrete', 'Glass', 'Steel', 'Water', 'Wood']
Popped elements is: Wood
After pop(): ['Brick', 'Ceramics', 'Concrete', 'Glass', 'Steel', 'Water']
After removing Glass: ['Brick', 'Ceramics', 'Concrete', 'Steel', 'Water']
After insert: ['Brick', 'Ceramics', 'Concrete', 'Stone', 'Steel', 'Water']

Result
Thus the Python program toprint materials required for construction of a building using list operation
was executed successfully.
Ex. No: 5.B) ELEMENTS OF A CIVIL STRUCTURE

Aim
To implement operations of dictionary to print elements of a civil structure

Algorithm
1. Start
2. Read elements in civil variable
3. Print elements
4. Insert element in last position and print
5. Update element in particular position and print the elements
6. Print the value of key using square bracket and get method
7. Remove element from the structure and print the remaining elements
8. Remove element arbitrarily using popitem( ) function
9. Stop

Program
civil = {1:'Foundation',2:'Roof',3:'Beams',4:'Columns',5:'Walls'};
print(civil)
civil[6]='Stairs'
print("Print elements after adding:",civil)
civil[3]='Lintels'
print("Elements after updating key 3:",civil)
print("Print value of Key 2:",civil[2])
print("Print value of Key 5:",civil.get(5))
print("Element removed from key 1:",civil.pop(1))
print("Elements after removal:",civil)
print("Element removed arbitrarily:",civil.popitem())
print("Elements after pop:",civil)
Output
{1: 'Foundation', 2: 'Roof', 3: 'Beams', 4: 'Columns', 5: 'Walls'}
Print elements after adding: {1: 'Foundation', 2: 'Roof', 3: 'Beams', 4: 'Columns', 5: 'Walls', 6: 'Stairs'}
Elements after updating key 3: {1: 'Foundation', 2: 'Roof', 3: 'Lintels', 4: 'Columns', 5: 'Walls', 6: 'Stairs'}
Print value of Key 2: Roof
Print value of Key 5: Walls
Element removed from key 1: Foundation
Elements after removal: {2: 'Roof', 3: 'Lintels', 4: 'Columns', 5: 'Walls', 6: 'Stairs'}
Element removed arbitrarily: (6, 'Stairs')
Elements after pop: {2: 'Roof', 3: 'Lintels', 4: 'Columns', 5: 'Walls'}

Result
Thus the Python program toprint elements of civil structure using dictionary operation
was executed successfully.
Ex. No: 6.A) FACTORIAL USING RECURSION

Aim
To write a program to find the factorial of a number using recursion
Algorithm
1. Start
2. Read num
3. Call fact (n)
4. Print factorial f
5. Stop
Fact (n)
1. if n==1 then
return n
2. else
return (n*fact (n-1))

Program
def fact (n):
if n==1:
return n
else:
return (n*fact (n-1))
num = int (input("Enter a number: "))
f=fact(num)
print("The factorial of",num,"is",f)

Output
Enter a number: 3
The factorial of 3 is 6

Result
Thus the Python program tofind factorial of a number using recursion was executed successfully.
Ex. No: 6.B) FINDING LARGEST NUMBER IN A LIST USING FUNCTION

Aim
To write a program to find the largest number in a list using functions.
Algorithm
1. Start
2. Initialize empty list as lis1t=[ ]
3. Read upper limit of list n
4. Read values of list using for loop
for i in range(1,n+1)
Read num
Append num in list1
5. Call function myMax(list1)
6. Stop
myMax(list1)
1. Print largest element in a list using max( ) function
Program
def myMax(list1):
print("Largest element is:", max(list1))
list1 = []
n = int(input("Enter number of elements in list: "))
for i in range(1, n + 1):
num = int(input("Enter elements: "))
list1.append(num)
myMax(list1)
Output
Enter number of elements in list: 4
Enter elements: 23
Enter elements: 15
Enter elements: 67
Enter elements: 45
Largest element is: 67
Result
Thus the Python program to find the largest number in a list using functions was executed successfully.
Ex. No: 6.C) FINDING AREA OF A SHAPE USING FUNCTION

Aim
To write a program to find the area of a shape using functions

Algorithm
1. Start
2. Print Calculate Area of Shape
3. Read shape_name
4. Call function calculate_area(shape_name)
5. Stop
calculate_area(name):
1. Convert letters into lowercase
2. Check name== “rectangle” :
2.1. Read l
2.2. Read b
2.3. Calculate rect_area=l*b
2.4. Print area of rectangle
3. elif name==”square”:
3.1. Read s
3.2. Calculate sqt_area = s * s
3.3. Print area of square
4. elif name == "triangle":
4.1. Read h
4.2. Read b
4.3. Calculate tri_area = 0.5 * b * h
4.4. Print area of triangle
5. elif name == "circle":
5.1. Read r
5.2. Calculate circ_area = pi * r * r
5.3. Print area of circle
6. else:
6.1. Print Sorry! This shape is not available
Program
def calculate_area(name):
name = name.lower()
if name == "rectangle":
l = int(input("Enter rectangle's length: "))
b = int(input("Enter rectangle's breadth: "))
rect_area = l * b
print ("The area of rectangle=", rect_area)
elif name == "square":
s = int(input("Enter square's side length: "))
sqt_area = s * s
print ("The area of square=",sqt_area)
elif name == "triangle":
h = int(input("Enter triangle's height length: "))
b = int(input("Enter triangle's breadth length: "))
tri_area = 0.5 * b * h
print ("The area of triangle=",tri_area)
elif name == "circle":
r = int(input("Enter circle's radius length: "))
pi = 3.14
circ_area = pi * r * r
print ("The area of circle=",circ_area)
else:
print("Sorry! This shape is not available")
print("Calculate Area of Shape")
shape_name = input("Enter the name of shape: ")
calculate_area(shape_name)
Output
Calculate Area of Shape
Enter the name of shape: rectangle
Enter rectangle's length: 2
Enter rectangle's breadth: 3
('The area of rectangle=', 6)

Result
Thus the Python program to find the area of a shape using functions was executed successfully.
Ex. No: 7.A) REVERSINGA STRING

Aim
To write a program to find reverse a string.

Algorithm
1. Start
2. Read string in s
3. Print reversed string through function reverse(s)
4. Stop
reverse(string):
1. Reverse a string using join(reversed(string))
2. return reversed string

Program
def reverse(string):
string = "".join(reversed(string))
return string
s = input("Enter any string: ")
print ("The original string is:",s)
print ("The reversed string(using reversed):",reverse(s))

Output
Enter any string: river
('The original string is:', 'river')
('The reversed string(using reversed):', 'revir')

Result
Thus the Python program to find reverse a string was executed successfully.
Ex. No: 7.B) PALINDROME IN A STRING

Aim
To write a program to check a given string is palindrome or not.

Algorithm
1. Start
2. Read string
3. Reverse a string using reversed( ) function
4. Check rev_string==original_string
Print "It is palindrome"
5. else
It is not palindrome
6. Stop

Program
string = input("Enter string: ")
rev_string = reversed(string)
if list(string)== list(rev_string):
print("It is palindrome")
else:
print("It is not palindrome")

Output
Enter string: mam
It is palindrome

Result
Thus the Python program to check a string palindrome was executed successfully.
Ex. No: 7.C) COUNT CHARACTERS IN A STRING

Aim
To write a program to count number of characters in a string.

Algorithm
1. Start
2. Read string
3. Read character to count
4. Count number of characters using count( ) function
5. Print count
6. Stop

Program
string = input("Enter any string: ")
char = input("Enter a character to count: ")
val = string.count(char)
print("count",val)

Output
Enter any string: river
Enter a character to count: r
('count', 2)

Result
Thus the Python program to count number of characters in a string was executed successfully.
Ex. No: 7.D) REPLACING CHARACTERS

Aim
To write a program to replace characters in a string.

Algorithm
1. Start
2. Read string
3. Read old string to replace in str1
4. Read new string in str2
5. replace(str1,str2)
6. Print replaced string
7. Stop

Program
string = input("Enter any string: ")
str1 = input("Enter old string: ")
str2 = input("Enter new string: ")
print(string.replace(str1, str2))

Output
Enter any string: python
Enter old string: p
Enter new string: P
Python

Result
Thus the Python program to replace characters in a string was executed successfully.

You might also like