Skip to content

← Back to Overview

Celsius to Fahrenheit Conversion

Concept Explanation

What is it?

This program converts a temperature value given in Celsius (℃) to its equivalent in Fahrenheit (℉). 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 reading a recipe from a European cookbook that lists oven temperatures in Celsius, you might need to convert it to Fahrenheit for your American oven. For example, 180℃ is approximately 356℉.


Algorithm

  1. Start.
  2. Get the temperature in Celsius (celsius) from the user.
  3. Apply the conversion formula: fahrenheit = (celsius * 9 / 5) + 32.
  4. Display the original Celsius temperature and the calculated Fahrenheit temperature.
  5. End.

Conversion Formula: Fahrenheit = (Celsius × 9/5) + 32

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: -273.15 °C is -459.67 °F.


Implementations

import java.util.Scanner;

public class CelsiusToFahrenheit {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter temperature in Celsius: ");
        double celsius = scanner.nextDouble();

        // Convert Celsius to Fahrenheit
        double fahrenheit = (celsius * 9 / 5) + 32;

        System.out.println("Celsius: " + celsius + " ℃");
        System.out.println("Fahrenheit: " + String.format("%.2f", fahrenheit) + " ℉");

        scanner.close();
    }
}
# Get Celsius temperature from user
celsius_str = input("Enter temperature in Celsius: ")
celsius = float(celsius_str)

# Convert Celsius to Fahrenheit
fahrenheit = (celsius * 9/5) + 32

print(f"Celsius: {celsius}°C")
print(f"Fahrenheit: {fahrenheit:.2f}°F") # Format to 2 decimal places
#include <stdio.h>

int main() {
    double celsius, fahrenheit;

    printf("Enter temperature in Celsius: ");
    scanf("%lf", &celsius);

    // Convert Celsius to Fahrenheit
    fahrenheit = (celsius * 9 / 5) + 32;

    printf("%.2lf Celsius is equal to %.2lf Fahrenheit\n", celsius, fahrenheit);

    return 0;
}
SET SERVEROUTPUT ON;
DECLARE
  celsius_val     NUMBER := &Enter_Celsius_Temperature;
  fahrenheit_val  NUMBER;
BEGIN
  DBMS_OUTPUT.PUT_LINE('--- Celsius to Fahrenheit Conversion ---');
  DBMS_OUTPUT.PUT_LINE('Celsius: ' || celsius_val || ' ℃');

  fahrenheit_val := (celsius_val * 9/5) + 32;

  DBMS_OUTPUT.PUT_LINE('Fahrenheit: ' || ROUND(fahrenheit_val, 2) || ' ℉');
END;
/

Explanation

  • Java: Uses double for temperature values to maintain precision. Scanner reads the Celsius 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 double input using scanf("%lf", ...). The formula is applied, and printf("%.2lf\n", ...) displays the formatted result.
  • Oracle: Implemented in PL/SQL. Uses NUMBER data type for temperature. Substitution variable (&Enter_Celsius_Temperature) for input. ROUND() is used for formatting the Fahrenheit 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 Celsius Temperature]
    B --> C[Calculate Fahrenheit = (Celsius × 9/5) + 32]
    C --> D[Display Celsius and Fahrenheit]
    D --> E[End]

Sample Dry Run

Step Celsius Fahrenheit Description
Input 20.0 - User enters 20.0
Process 20.0 68.0 Fahrenheit = (20.0 * 9 / 5) + 32 = 68.0
Output 20.0 68.0 Display "20.00 ℃ is 68.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 only source of knowledge is experience." - Albert Einstein