Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
22 views

Python Program Algorith2

Uploaded by

Gokulakrishnan
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)
22 views

Python Program Algorith2

Uploaded by

Gokulakrishnan
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/ 18

Python Program Algorithm

1.Temperature Conversion
Aim :
To create a menu driven program to convert the given
temperature from Fahrenheit from Celsius and vice-versa
depending upon users choice.
Algorithm :
Step 1: Start the program.
Step 2: Declare a variable temp and read the input
temperature value from the user.
Step 3: If the user specifies the input temperature in
Fahrenheit (a == 'f'):
Step 4: Calculate the converted temperature in Celsius:
con_temp = (temp - 32) * 5/9.
Otherwise (if the user specifies the input temperature in
Celsius):
Step 5: Calculate the converted temperature in Fahrenheit:
con_temp = (9/5) * temp + 32.
Step 6: Display the converted temperature (con_temp).
Step 7: End the program.
2. Calculating Area of Different Shapes With Python

Aim :
To create a menu driven program using user defined function
to find the area of rectangle , square , circle , and triangle by
accepting suitable input parameters from user.
Algorithm :
Step 1: Print the menu with options for different shapes:
square, rectangle, triangle, and circle.
• Input Shape Option:
• Read the user’s choice (1, 2, 3, or 4) for the shape they
want to calculate the area for.
Step 2: Calculate Area:
• If the user selects option 1 (square)
• Calculate the area of the square using the formula: area
= side * side.
• Display the result.
Step 3: If the user selects option 2 (rectangle):
• Calculate the area of the rectangle using the formula:
area = length * breadth.
• Display the result.
Step 4: If the user selects option 3 (triangle):
• Calculate the area of the triangle using the formula: area
= 0.5 * base * height.
• Display the result.
Step 5: If the user selects option 4 (circle):
• Calculate the area of the circle using the formula: area =
π * radius^2 (where π is approximately 3.14).
• Display the result.
Step 6: Invalid Input Handling:
• If the user enters an invalid option (other than 1, 2, 3, or
4), display an error message: “Enter valid input.”
Step 7: End the program.
3.Fibonacci series

Aim:
To write a python program to display the first n terms of
Fibonacci series.
Algorithm :
Step 1:Print the message “Fibonacci series.”
Step 2: Declare the variable n And Initialize Variables:
• n1 with the value 0 (first term of the Fibonacci series).
• n2 with the value 1 (second term of the Fibonacci
series).
• count with the value 0 (to keep track of the number of
terms printed).
Step 3: Generate Fibonacci Series:
Step 4: Use a for loop to iterate from count to n-1 (since we
already have the first two terms).Inside the loop , Print the
value of n1 (the current term) without a newline.
Step 5: Calculate the next term nth by adding n1 and n2.
Step 6: Update the values of n1 and n2:
• n1 becomes the old value of n2.
• n2 becomes the new value of nth.
• Increment count by 1.
Step 7: End the program.
4.Factorial Of The Given Number

Aim:
To write a python program to find factorial of the given
number.
Algorithm:
Step 1: Define a function named factorial.The function will
calculate the factorial of n.
Step 2: Declare an integer argument n.
Step 3: If n is equal to 1 or 2, return 1 (since the factorial of 1
and 2 is 1).Otherwise, calculate the factorial recursively.
Step 3: Multiply n by the factorial of (n-1).
Step 5: Prompt the user to input an integer value (num).
Step 6: Call the factorial function with the input value num.
Print the result: “Factorial of num is factorial(num).
Step 7: End the program.
5.Sum Of Series

Aim: To write a python program to find some of the


following series for n terms.
Algorithm:
Step 1: Declare an integer value (n).
Step 2: Initialize Variables.
• fact with the value 1 (to store the factorial of each term).
• sum with the initial value 0 (to accumulate the
alternating sum).
Step 3: Calculate Factorial and Alternating Sum.
Step 4: Use a for loop to iterate from 1 to n.Inside the loop ,
Update the fact by multiplying it with the current value of i.If i
is odd (i.e., i % 2 == 1):
Step 4: Add the fraction (i / fact) to the sum.
Step 5: Print the term in the format i / i! -.Otherwise (if i is
even):Subtract the fraction (i / fact) from the sum.
Step 6:Print the term in the format i / i! +.
Step 7: Print the Final Result.After the loop, print the
accumulated alternating sum.
Step 8: End the program.
6.Sum And Product Of Two Compatible Matrices

Aim:
To write a python program to calculate the sum and product
of two compatible matrices.
Algorithm:
Step 1: Declare an input for the number of rows (row1) and
columns (col1) for the first matrix (m1).Read the elements of
the first matrix (m1).
Step 2: Declare an input for the number of rows (row2) and
columns (col2) for the second matrix (m2).Read the elements
of the second matrix (m2).
Step 3: Initialize Result Matrix:
• Create an empty result matrix (res) with dimensions
row1 × col1.
• Check Validity , If the dimensions of the two matrices
(m1 and m2) are not compatible for addition (i.e., row1
!= row2 or col1 != col2), print “Matrix addition is not
possible” and return.Otherwise, proceed to perform
matrix addition.
Step 4: Addition:
• For each element at position (i, j) in the result matrix
(res):
• Calculate res[i][j] = m1[i][j] + m2[i][j].
Step 5: Print the resulting matrix (res).
Step 6: Matrix Multiplication.Prompt the user to input the
number of rows (row1) and columns (col1) for the first matrix
(m1).Read the elements of the first matrix (m1).
Step 7: Prompt the user to input the number of rows (row2)
and columns (col2) for the second matrix (m2).Read the
elements of the second matrix (m2).
Step 8: Initialize Result Matrix:
Step 9: Create an empty result matrix (res) with dimensions
row1 × col2.
Step 10: Check Validity , If the dimensions of the two
matrices (m1 and m2) are not compatible for multiplication
(i.e., col1 != row2), print “Matrix multiplication is not
possible” and return.Otherwise, proceed to perform matrix
multiplication.
Step 11: Multiplication:
• For each element at position (i, j) in the result matrix
(res):
• Initialize res[i][j] to 0.
• Calculate res[i][j] by summing the products of
corresponding elements from m1 and m2: res[i][j] +=
m1[i][k] * m2[k][j] for all k from 0 to col1-1.
Step 12: Print the resulting matrix (res).
Step 13: Prompt the user to choose between addition (1) or
multiplication (2).Call the appropriate function (matrixadd or
matrixmultiply based on the user’s choice.
Step 14: End the program.
7.String Function

Aim:
To write a python program to explore string function.
Algorithm:
Step 1: Initialize a string variable str with the value
'HelloEveryone'.
Step 2: Print the string.Access First and Last Characters.
Step 3: Print the first character of the string: print('First
character =', str[0])
Step 4: Print the last character of the string using negative
indexing: print('Last character =', str[-1]).
Step 5: Access Substring (2nd to 5th Characters).Print the
substring from the 2nd to the 4th character (Python uses 0-
based indexing): print('2nd to 5th character =', str[1:4]).
Step 6: Calculate the total number of characters in the string
using the len() function: print('Total number of characters =',
len(str)).
Step 7: Convert the entire string to lowercase using the
lower() method: print('Lowercase =', str.lower()).
Step 8: Convert the entire string to uppercase using the
upper() method: print('Uppercase =', str.upper()).
Step 9: String Concatenation:
• Initialize two string variables s1 with the value
'computer' and s2 with the value 'science'.
• Concatenate the two strings using the + operator:
print('String concatenation =', s1 + s2).
Step 10: End the program.
8.Create And Read A CSV File Based On User Input

Aim:
To write a python program to create and read a csv file and
display the file contents.
Algorithm:
Step 1:Open CSV File for Writing.
Step 2: Use the open() function to open a CSV file named
'file.csv' in write mode ('w').
Step 3: Create a file object named csvfile.
Step 4: Write Data to CSV File.For each value of i in the
range from 1 to 4.
Step 5: Declare the input and the register number
(rn).Prompt the user to input the name (n).
Step 6: Create a list named save containing the register
number and name: save = [rn, n].Use the csv.writer object
to write the save list as a row in the CSV file.
Step 7: Close the CSV file using the close() method.
Step 8: Open CSV File for Reading.Use the open() function
to open the CSV file named 'file.csv' in read mode ('r').
Step 9: Create a file object named csvfile.Read Data from
CSV File:
• Create a csv.reader object named reader using the
csvfile.For each row in the reader.
Step 10: Print the row.
Step 11: End the program.
9.Hello Python

Aim:
To write a python program to write the text “Hello Python”
in an existing file.
Algorithm:
Step 1: Open the File for Reading.Use the open() function
with the file name 'readme.txt' and mode 'r' (read) to open
the file.
Step 2: Create a file object named f.
Step 3: Read the entire content of the file using the read()
method and Store the content in the variable contents.
Step 4: Print the contents of the file.
Step 5: Open the File for Writing.Use the open() function
with the file name 'readme.txt' and mode 'w' (write) to
open the file.
Step 6: Create a file object named f.
Step 7: Write the line 'howtowritetextfileinpython' to the
file and Add a newline character ('\n') after writing the line.
Step 8: Print a message indicating that the file has been
written to and overwritten.
Step 9: Open the File for Appending.
Step 10: Use the open() function with the file name
'readme.txt' and mode 'a' (append) to open the file and
Create a file object named f.
Step 11: Write the line 'hellopython' to the file and Add a
newline character ('\n') after writing the line.
Step 12: Print a message indicating that the file has been
appended to.
Step 13: To reading the Appended Contents:
Open the File for Reading Again:
• Use the open() function with the file name 'readme.txt'
and mode 'r' (read) to open the file.
Step 14: Create a file object named f.Read to read the
Contents Again:
• Read the entire content of the file using the read()
method.
• Store the content in the variable content.
Step 15: Print the contents of the file after appending.
Step 16: End the program.
10.Turtle In Python

Aim: To create a python program to set background color


and draw a circle using turtle module.
Algorithm:
Step 1: Import the turtle module to use its drawing
capabilities.
Step 2: Create a turtle object named Pen.
Step 3: Define the ring Function.The ring function takes
two arguments: col (color) and rad (radius).
Step 4: Set the fill color of the turtle to col and Begin filling
the shape.
Step 5: Draw a circle with the specified radius (rad) and
End the filling.
Step 6: Lift the pen (up) to move to a new position.
Step 7: Draw two black rings at positions (-35, 95) and (35,
95) with a radius of 15.
Step 8: Draw a white ring at position (0, 35) with a
radius of 40.
Step 9: Draw two small black rings at positions (-18, 75)
and (18, 75) with a radius of 8.
Step 10: Draw two small white rings at positions (-18, 77)
and (18, 77) with a radius of 4.
Step 11: Draw a black circle at position (0, 55) with a
radius of 5.
Step 12: Draw two semicircles (top and bottom halves) at
position (0, 55) with a radius of 5.
Step 13: Hide the turtle cursor using pen.hideturtle().
Step 14: End the program.

------------------------------------------------------------------------------

You might also like