Saturday, 14 December 2024

Day 40: Python Program to Convert Celsius to Fahrenheit


def celsius_to_fahrenheit(celsius):

    fahrenheit = (celsius * 9/5) + 32

    return fahrenheit

celsius = float(input("Enter temperature in Celsius: "))

fahrenheit = celsius_to_fahrenheit(celsius)

print(f"{celsius}°C is equal to {fahrenheit}°F")

 #source code --> clcoding.com 

Code Explanation:

1. Function: celsius_to_fahrenheit
def celsius_to_fahrenheit(celsius):
    fahrenheit = (celsius * 9/5) + 32
    return fahrenheit
Purpose:
Converts a given temperature in Celsius to Fahrenheit.

Parameter:
celsius: A float or integer representing the temperature in Celsius.

Formula:
The conversion formula from Celsius to Fahrenheit is:

F=(C×9/5)+32

Where:
๐น
F is the temperature in Fahrenheit.
๐ถ
C is the temperature in Celsius.

Steps:
Multiply the Celsius temperature by 9/5.
Add 32 to the result.
Return the calculated Fahrenheit temperature.

2. User Input
celsius = float(input("Enter temperature in Celsius: "))

input():
Prompts the user to enter a temperature value in Celsius.

float():
Converts the input string to a floating-point number, allowing decimal values.

celsius:
Stores the user-provided temperature.

3. Function Call
fahrenheit = celsius_to_fahrenheit(celsius)
The program calls the celsius_to_fahrenheit function, passing the celsius value entered by the user as an argument.
The function returns the equivalent temperature in Fahrenheit, which is stored in the variable fahrenheit.

4. Output
print(f"{celsius}°C is equal to {fahrenheit}°F")
Formatted String (f-string):
Embeds the values of celsius and fahrenheit into the output string.
Displays the conversion result in a user-friendly format.

 

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (41) AI (33) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (173) C (77) C# (12) C++ (82) Course (67) Coursera (225) Cybersecurity (24) data management (11) Data Science (128) Data Strucures (8) Deep Learning (20) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (34) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (59) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (3) Pandas (4) PHP (20) Projects (29) Python (925) Python Coding Challenge (343) Python Quiz (12) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (42) UX Research (1) web application (8)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses