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

Python Programs With Algorithm

The document outlines four Python programs: one for temperature conversion between Fahrenheit and Celsius, another for printing a star pattern using nested loops, a third for calculating grades based on marks from five subjects, and a fourth for calculating the area of different shapes. Each program includes an algorithm and the corresponding Python code. The programs prompt user input and display results based on the calculations performed.

Uploaded by

kalaivanant
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 views5 pages

Python Programs With Algorithm

The document outlines four Python programs: one for temperature conversion between Fahrenheit and Celsius, another for printing a star pattern using nested loops, a third for calculating grades based on marks from five subjects, and a fourth for calculating the area of different shapes. Each program includes an algorithm and the corresponding Python code. The programs prompt user input and display results based on the calculations performed.

Uploaded by

kalaivanant
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/ 5

1.

Temperature Conversion (Fahrenheit <-> Celsius)

Algorithm:

1. Start

2. Ask user to choose conversion type

3. If choice is 1, convert Fahrenheit to Celsius

4. If choice is 2, convert Celsius to Fahrenheit

5. Display the result

6. End

Python Program:

print("1: Fahrenheit to Celsius")


print("2: Celsius to Fahrenheit")
choice = int(input("Enter your choice (1 or 2): "))

if choice == 1:
f = float(input("Enter temperature in Fahrenheit: "))
c = (f - 32) * 5/9
print("Temperature in Celsius:", c)
elif choice == 2:
c = float(input("Enter temperature in Celsius: "))
f = (c * 9/5) + 32
print("Temperature in Fahrenheit:", f)
else:
print("Invalid choice")
2. Pattern Printing Using Nested Loop

Algorithm:

1. Start

2. Loop to print increasing stars from 1 to 5

3. Loop to print decreasing stars from 4 to 1

4. Use nested loop to print * based on line number

5. End

Python Program:

# Increasing part
for i in range(1, 6):
for j in range(i):
print("*", end="")
print()

# Decreasing part
for i in range(4, 0, -1):
for j in range(i):
print("*", end="")
print()
3. Grade Calculation Based on Marks

Algorithm:

1. Start

2. Input marks for 5 subjects

3. Calculate total and percentage

4. Check percentage range and assign grade

5. Display total, percentage, and grade

6. End

Python Program:

marks = []
for i in range(5):
m = float(input(f"Enter marks for subject {i+1}: "))
marks.append(m)

total = sum(marks)
percentage = total / 5

if percentage >= 80:


grade = 'A'
elif percentage >= 70:
grade = 'B'
elif percentage >= 60:
grade = 'C'
elif percentage >= 40:
grade = 'D'
else:
grade = 'E'

print("Total Marks:", total)


print("Percentage:", percentage)
print("Grade:", grade)
4. Area Calculation (Rectangle, Square, Circle, Triangle)

Algorithm:

1. Start

2. Ask user to choose shape

3. Based on choice, input required dimensions

4. Apply formula and calculate area

5. Display result

6. End

Python Program:

print("1. Rectangle")
print("2. Square")
print("3. Circle")
print("4. Triangle")

choice = int(input("Enter your choice: "))

if choice == 1:
l = float(input("Enter length: "))
b = float(input("Enter breadth: "))
area = l * b
print("Area of Rectangle:", area)
elif choice == 2:
s = float(input("Enter side: "))
area = s * s
print("Area of Square:", area)
elif choice == 3:
r = float(input("Enter radius: "))
area = 3.14159 * r * r
print("Area of Circle:", area)
elif choice == 4:
b = float(input("Enter base: "))
h = float(input("Enter height: "))
area = 0.5 * b * h
print("Area of Triangle:", area)
else:
print("Invalid choice")

You might also like