Fahrenheit to Celsius Conversion¶
Concept Explanation¶
What is it?¶
This program converts a temperature value given in Fahrenheit (℉) to its equivalent in Celsius (℃). These are two common scales for measuring temperature, and conversion between them is a standard operation.
Why is it important?¶
Temperature conversion is a practical application of basic arithmetic and variable manipulation in programming. It's often encountered in scientific, meteorological, and everyday applications where data might be provided in one unit but needed in another.
Where is it used?¶
- Weather Applications: Displaying local weather in preferred units.
- Scientific Research: Converting data collected in different temperature scales.
- Industrial Processes: Monitoring and controlling temperatures in manufacturing, where specific units might be used.
- International Travel: Converting temperatures encountered in different countries.
Real-world example¶
If you're traveling from the United States (which primarily uses Fahrenheit) to Europe (which primarily uses Celsius), you might hear the weather forecast is 25℃. To understand what that feels like, you'd convert it to Fahrenheit. Or, if you know it's 77℉ in the US, you can convert it to 25℃ for someone in Europe.
Algorithm¶
- Start.
- Get the temperature in Fahrenheit (
fahrenheit) from the user. - Apply the conversion formula:
celsius = (fahrenheit - 32) * 5 / 9. - Display the original Fahrenheit temperature and the calculated Celsius temperature.
- End.
Conversion Formula:
Celsius = (Fahrenheit - 32) × 5/9
Edge Cases: - Non-numeric input for temperature (handled by language-specific error mechanisms or explicit validation). - Extreme temperatures (very high or very low) should still convert correctly. - Absolute zero: -459.67 °F is -273.15 °C.
Implementations¶
import java.util.Scanner;
public class FahrenheitToCelsius {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter temperature in Fahrenheit: ");
double fahrenheit = scanner.nextDouble();
// Convert Fahrenheit to Celsius
double celsius = (fahrenheit - 32) * 5 / 9;
System.out.println("Fahrenheit: " + fahrenheit + " ℉");
System.out.println("Celsius: " + String.format("%.2f", celsius) + " ℃");
scanner.close();
}
}
# Get Fahrenheit temperature from user
fahrenheit_str = input("Enter temperature in Fahrenheit: ")
fahrenheit = float(fahrenheit_str)
# Convert Fahrenheit to Celsius
celsius = (fahrenheit - 32) * 5 / 9
print(f"Fahrenheit: {fahrenheit}°F")
print(f"Celsius: {celsius:.2f}°C") # Format to 2 decimal places
SET SERVEROUTPUT ON;
DECLARE
fahrenheit_val NUMBER := &Enter_Fahrenheit_Temperature;
celsius_val NUMBER;
BEGIN
DBMS_OUTPUT.PUT_LINE('--- Fahrenheit to Celsius Conversion ---');
DBMS_OUTPUT.PUT_LINE('Fahrenheit: ' || fahrenheit_val || ' ℉');
celsius_val := (fahrenheit_val - 32) * (5/9);
DBMS_OUTPUT.PUT_LINE('Celsius: ' || ROUND(celsius_val, 2) || ' ℃');
END;
/
Explanation¶
- Java: Uses
doublefor temperature values to maintain precision.Scannerreads the Fahrenheit input, and the formula is applied directly.String.format("%.2f", ...)formats the output. - Python: Reads input as a string and converts to
float. Applies the formula and uses an f-string to display formatted output. - C: Reads
doubleinput usingscanf("%lf", ...). The formula is applied, andprintf("%.2lf\n", ...)displays the formatted result. - Oracle: Implemented in PL/SQL. Uses
NUMBERdata type for temperature. Substitution variable (&Enter_Fahrenheit_Temperature) for input.ROUND()is used for formatting the Celsius output.
Complexity Analysis¶
- Time Complexity: O(1) - The number of arithmetic operations is constant.
- Space Complexity: O(1) - A fixed number of variables are used.
Flowchart¶
graph TD
A[Start] --> B[Get Fahrenheit Temperature]
B --> C[Calculate Celsius = (Fahrenheit - 32) × 5/9]
C --> D[Display Fahrenheit and Celsius]
D --> E[End]
Sample Dry Run¶
| Step | Fahrenheit | Celsius | Description |
|---|---|---|---|
| Input | 68.0 | - | User enters 68.0 |
| Process | 68.0 | 20.0 | Celsius = (68.0 - 32) * 5 / 9 = 20.0 |
| Output | 68.0 | 20.0 | Display "68.00 ℉ is 20.00 ℃" |
| End | - | - | Program terminates |
Practice Problems¶
Easy¶
- Modify the program to allow the user to choose between converting Fahrenheit to Celsius or Celsius to Fahrenheit.
Medium¶
- Implement input validation to ensure temperature values are within a reasonable range (e.g., above absolute zero).
- Convert temperature between Kelvin and Celsius/Fahrenheit.
Hard¶
- Create a temperature conversion utility that handles multiple units (e.g., Rankine, Réaumur) and displays a conversion table.
"The function of a good software is to make the complex appear simple." - Grady Booch