Python programs on input & output statements

Pogram 1: Write a python program to read a float value and convert Fahrenheit to Centigrade.

Formula:

\[ ^o C = \frac{5}{9} \times (^oF-32) \]

Program:

                                    
                                    
# Read temperature in Fahrenheit from user
fahrenheit = float(input("Enter temperature in Fahrenheit: "))

# Convert to Centigrade (Celsius)
centigrade = (fahrenheit - 32) * 5/9

# Display the result
print(str(fahrenheit) + "°F is equal to " + str(round(centigrade, 2)) + "°C")
                                        
                                    

Output:

Enter temperature in Fahrenheit: 98.6
98.6°F is equal to 37.00°C

Pogram 2: Write a python program to find the area of triangle.

Formula: Area of the traingle is given by

\[ Area = \frac{1}{2} \times base \times height \]

Program:

                                    
                                    
# Read base and height from user
base = float(input("Enter the base of the triangle: "))
height = float(input("Enter the height of the triangle: "))

# Area
area = 0.5 * base * height

# Display the result
print("Area of the triangle is:", area)
                                        
                                    

Output:

Enter the base of the triangle: 2
Enter the height of the triangle: 2
Area of the triangle is: 2

Pogram 3: Write a python program to read the marks in four subjects and display the average.

Formula:

\[ Average = \frac{S1+S2+S3+S4}{4} \] Where S1, S2, S3 and S4 are marks of the related subjects.

Program:

                                    
                                    
# Read marks for 4 subjects
S1 = float(input("Enter marks for Subject 1: "))
S2 = float(input("Enter marks for Subject 2: "))
S3 = float(input("Enter marks for Subject 3: "))
S4 = float(input("Enter marks for Subject 4: "))

# Calculate total and average
total = S1+S2+S3+S4
average = total / 4

# Display the result
print("Average Marks of the four students",average")
                                        
                                    

Output:

Enter marks for Subject 1: 85
Enter marks for Subject 2: 92
Enter marks for Subject 3: 78
Enter marks for Subject 4: 88
Average Marks of the four students: 85.75