Write a temperature conversion program which converts Fahrenheit to Celsius and Celsius to Fahrenheit in Java



In this core java programming tutorial we will write a temperature conversion program.





Write a temperature conversion program which converts
 > Fahrenheit to Celsius and
 > Celsius to Fahrenheit
using switch statement in Java.


Full Program/SourceCode / Example to write a temperature conversion program which converts Fahrenheit to Celsius and Celsius to Fahrenheit using switch statement in Java >
import java.util.Scanner;
/*
* Write a temperature conversion program which converts
* > Fahrenheit to Celsius and
* > Celsius to Fahrenheit
* using switch statement in Java.
*/
/** JavaMadeSoEasy.com */
public class TemperatureConversionProgramUsingSwitchStatementExample {
   public static void main(String[] args) {
          Scanner scanner = new Scanner(System.in);
          System.out.print("Enter 1 for Fahrenheit to Celsius \n"
                       + "Enter 2 for Celsius to Fahrenheit conversion : ");
          int operation = scanner.nextInt();
         
          System.out.print("Enter temperature : ");
          float temperature = scanner.nextFloat();
         
          float convertedTemperature = 0f;
         
          /*
          * Perform temperature conversion using switch statement in Java.
          */
          switch (operation) {
         
          //Fahrenheit to Celsius temperature conversion
          case 1:
                 convertedTemperature = ((temperature - 32) * 5) / 9;
                 System.out.println(temperature + " Fahrenheit " + " in Celsius = "
                              + convertedTemperature);
                 break;
                
          //Celsius to Fahrenheit temperature conversion
          case 2:
                 convertedTemperature = (9 * temperature / 5) + 32;
                 System.out.println(temperature + " Celsius " + " in Fahrenheit = "
                              + convertedTemperature);
                 break;
          default: // optional
                 System.out.println("Enterd temerature is Invalid");
                 break; // optional
          }
   }
}
/*OUTPUT
Enter 1 for Fahrenheit to Celsius
Enter 2 for Celsius to Fahrenheit conversion : 1
Enter temperature : 100
100.0 Fahrenheit  in Celsius = 37.77778
*/
/*
Enter 1 for Fahrenheit to Celsius
Enter 2 for Celsius to Fahrenheit conversion : 2
Enter temperature : 37.77
37.77 Celsius  in Fahrenheit = 99.986
*/


So, in this core java programming tutorial we wrote a temperature conversion program which converted Fahrenheit to Celsius and Celsius to Fahrenheit using switch statement in Java.


Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.


RELATED LINKS>































eEdit
Must read for you :