Java Program to Convert Celsius to Fahrenheit

In this tutorial, you will learn how to write a Java program to convert Celsius to Fahrenheit. We will break this down into three steps:

  • Understanding the formula used for temperature conversion.
  • Writing a basic program using a predefined Celsius value.
  • Making it interactive by accepting user input via the Scanner class.

Celsius to Fahrenheit Conversion

Celsius and Fahrenheit are two commonly used temperature scales. To convert a temperature from Celsius to Fahrenheit, we use the following formula:

Fahrenheit = (Celsius × 9 / 5) + 32

Where:

  • Celsius represents the temperature in Celsius.
  • Fahrenheit represents the converted temperature in Fahrenheit.
  • 9/5 is the conversion factor between the two temperature scales.
  • 32 is added as part of the Celsius-to-Fahrenheit conversion formula.

Example:

Let’s consider an example where the temperature is 25°C. We will convert it into fahrenheit.

Using the formula:

Fahrenheit = (25 × 9 / 5) + 32
= 45 + 32
= 77°F

Therefore, 25°C is equal to 77°F.

Java Program to Convert Celsius to Fahrenheit

Problem Statement: Write a Java program to convert a temperature from Celsius to Fahrenheit by assigning a predefined value directly to a variable.

Complete Java Program

// Java program to convert Celsius to Fahrenheit.
public class CelsiusToFahrenheit {
    public static void main(String[] args) {
        // Store the temperature in Celsius.
        double celsius = 25.0;

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

        // Display the temperatures.
        System.out.println("Temperature in Celsius: " + celsius + "°C");
        System.out.println("Temperature in Fahrenheit: " + fahrenheit + "°F");
    }
}

Expected Output:

Temperature in Celsius: 25.0°C 
Temperature in Fahrenheit: 77.0°F

In this Java program:

  • We have created a class named CelsiusToFahrenheit.
  • The main() method is the starting point of the program.
  • The double variable celsius stores the temperature in Celsius. We have assigned a value 25.0 as the Celsius temperature.
  • The expression (celsius * 9 / 5) + 32 converts Celsius into Fahrenheit.
  • The converted temperature is stored in the fahrenheit variable.
  • System.out.println() displays the Celsius and Fahrenheit temperatures on the console.
  • We use the double data type so that the program can handle decimal temperature values.

The most important statement in the program is:

double fahrenheit = (celsius * 9 / 5) + 32;

This statement applies the Celsius-to-Fahrenheit conversion formula and stores the result in the fahrenheit variable.

Java Program to Convert Celsius to Fahrenheit Using Scanner

Problem Statement: Write a Java program to accept a temperature in Celsius from the user using the Scanner class, pass it to a user-defined instance method to perform the calculation, and display the converted Fahrenheit temperature.

Complete Java Program

import java.util.Scanner;

public class CelsiusToFahrenheitConverter {

    // User-defined instance method to convert Celsius to Fahrenheit
    public double convertToFahrenheit(double celsius) {
        return (celsius * 9 / 5) + 32;
    }

    public static void main(String[] args) {
        // Create a Scanner object to read input from the user
        Scanner scanner = new Scanner(System.in);

        // Prompt the user to enter the temperature in Celsius
        System.out.print("Enter temperature in Celsius: ");
        double celsius = scanner.nextDouble();

        // Create an instance of the class to call the instance method
        CelsiusToFahrenheitConverter converter = new CelsiusToFahrenheitConverter();

        // Call the user-defined instance method
        double fahrenheit = converter.convertToFahrenheit(celsius);

        // Display the results
        System.out.println("Temperature in Celsius: " + celsius + "°C");
        System.out.println("Temperature in Fahrenheit: " + fahrenheit + "°F");

        // Close the scanner resource
        scanner.close();
    }
}

Expected Output:

Enter temperature in Celsius: 25
Temperature in Celsius: 25.0°C
Temperature in Fahrenheit: 77.0°F

In this program:

  • The statement import java.util.Scanner; imports the Scanner class to enable reading input entered by the user from the console.
  • The Scanner object is used to accept keyboard input from the user.
  • We have declared a user-defined instance method convertToFahrenheit() that accepts the Celsius temperature as a parameter, applies the core conversion formula (celsius * 9 / 5) + 32, and returns the resulting Fahrenheit value.
  • The scanner.nextDouble(); prompts the user to type a temperature from the console and stores the entered decimal value in the celsius variable.
  • We have instantiated an object of the class (new CelsiusToFahrenheitConverter()).
  • Then, we have called the convertToFahrenheit() method using the newly created object, passed the user’s input, and stored the returned result in the fahrenheit variable.
  • The statement System.out.println(); outputs both the original Celsius value and the calculated Fahrenheit value to the console in a readable format.
  • The scanner.close(); closes the scanner stream to prevent memory leaks and good programming practice.

Key Points to Remember

  • The Celsius-to-Fahrenheit formula is Fahrenheit = (Celsius × 9 / 5) + 32.
  • Use the double data type when the temperature may contain decimal values.
  • The Scanner class can be used to accept temperature input dynamically from the user’s keyboard.
  • The nextDouble() method reads an entered decimal or numeric value from the user’s keyboard.
DEEPAK GUPTA

DEEPAK GUPTA

Deepak Gupta is the Founder of Scientech Easy, a Full Stack Developer, and a passionate coding educator with 8+ years of professional experience in Java, Python, web development, and core computer science subjects. With strong expertise in full-stack development, he provides hands-on training in programming languages and in-demand technologies at the Scientech Easy Institute, Dhanbad.

He regularly publishes in-depth tutorials, practical coding examples, and high-quality learning resources for both beginners and working professionals. Every article is carefully researched, technically reviewed, and regularly updated to ensure accuracy, clarity, and real-world relevance, helping learners build job-ready skills with confidence.