C Programs Tutorials | IT Developer
IT Developer

Java Programs



Share with a Friend

Operators & Expressions

Java Program: Temperature Unit Converter with Precise Formatting

import java.util.Scanner;

 

class TemperatureConverter {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

 

        System.out.println("Temperature Converter");

        System.out.println("1. Celsius to Fahrenheit");

        System.out.println("2. Fahrenheit to Celsius");

        System.out.println("3. Celsius to Kelvin");

        System.out.println("4. Kelvin to Celsius");

        System.out.print("Choose option (1-4): ");

 

        int choice = sc.nextInt();

        double temp, result;

 

        switch (choice) {

            case 1:

                System.out.print("Enter Celsius: ");

                temp = sc.nextDouble();

                result = (temp * 9 / 5) + 32;

                System.out.printf("Result: %.2f °F%n", result);

                break;

 

            case 2:

                System.out.print("Enter Fahrenheit: ");

                temp = sc.nextDouble();

                result = (temp - 32) * 5 / 9;

                System.out.printf("Result: %.2f °C%n", result);

                break;

 

            case 3:

                System.out.print("Enter Celsius: ");

                temp = sc.nextDouble();

                result = temp + 273.15;

                System.out.printf("Result: %.2f K%n", result);

                break;

 

            case 4:

                System.out.print("Enter Kelvin: ");

                temp = sc.nextDouble();

                result = temp - 273.15;

                System.out.printf("Result: %.2f °C%n", result);

                break;

 

            default:

                System.out.println("Invalid Choice!");

        }

 

        sc.close();

    }

}

Output

INPUT :
Temperature Converter
1. Celsius to Fahrenheit
2. Fahrenheit to Celsius
3. Celsius to Kelvin
4. Kelvin to Celsius
Choose option (1-4): 1
Enter Celsius: 36.6

OUTPUT : 
Temperature in Fahrenheit: 97.88 °F

Explanation

  1. Import Statement

import java.util.Scanner;

  • Imports the Scanner class to take input from the user.
  1. Class Declaration

class TemperatureConverter {

  • Defines a class named TemperatureConverter.
  1. Main Method

public static void main(String[] args)

  • Entry point of the Java program.
  1. Scanner Object

Scanner sc = new Scanner(System.in);

  • Creates a Scanner object to read user input from the keyboard.
  1. Menu Display

System.out.println("1. Celsius to Fahrenheit");

...

  • Displays temperature conversion options.
  1. Switch Case Logic

switch (choice)

  • Executes the selected conversion based on the user's choice.
  1. Conversion Formulas

Conversion

Formula

Celsius → Fahrenheit

(C × 9/5) + 32

Fahrenheit → Celsius

(F − 32) × 5/9

Celsius → Kelvin

C + 273.15

Kelvin → Celsius

K − 273.15

  1. Precise Formatting

System.out.printf("%.2f", result);

  • Displays the result up to 2 decimal places using printf().
  1. Scanner Close

sc.close();