Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

How to convert temperature degrees Fahrenheit to degrees Celsius and degrees Celsius to degrees Fahrenheit in a Java program ?.

Program to demonstrate how to convert temperature degrees Fahrenheit to degrees Celsius and degrees Celsius to degrees Fahrenheit in Java.

import java.util.Scanner;

public class TemperatureConversion {

 public static void main(String[] args) {

  // Creating a Scanner object using a constructor
  // which takes the System.in object. There are various
  // constructors for Scanner class for different
  // purposes. We want to use Scanner class to read
  // data from console on the system, So we are using
  // a constructor which is taking an System.in object
  Scanner scanner = new Scanner(System.in);

  // Prompt the user to enter temperature in Fahrenheit
  System.out.print("Enter temperature in Fahrenheit : ");

  double fahrenheit = scanner.nextDouble(); 

  // Convert Fahrenheit temperature to Celsius temperature
  // Formula : Celsius = (5/9)*(Fahrenheit - 32)

  double celsius = (5.0 / 9.0) * (fahrenheit - 32);

  // Displaying conversion on the console.
  System.out.println(fahrenheit + " degrees Fahrenheit is equal to " 
    + celsius + " degrees Celsius");

  System.out.println();

  // Prompt the user to enter temperature in Celsius
  System.out.print("Enter temperature in Celsius : ");

  celsius = scanner.nextDouble(); 

  // Convert Celsius temperature to Fahrenheit temperature
  // Formula : Fahrenheit = (9/5)*(Celsius + 32)

  fahrenheit = (9.0 / 5.0) * celsius + 32;

  // Displaying conversion on the console.
  System.out.println(celsius + " degrees Celsius is equal to " 
    + fahrenheit + " degrees Fahrenheit");
 }

}



Output of the program : 

 
 
© 2021 Learn Java by Examples Template by Hubberspot