Pin It

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 : 

 
Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
 
© 2012 Learn Java by Examples Template by Hubber Spot