Temperature Conversion
You are given the temperature T of an object in one of Celsius, Fahrenheit, and Kelvin scales.
Write a program to print T in all scales viz Celsius, Fahrenheit, and Kelvin.
Answers
Formula:
Formula to convert from Fahrenheit F to Celsius C is C = (F - 32) * 5 / 9. Formula to convert from Kelvin K to Celsius C is C = K - 273.
This program is in Java. I have also added all the possible outputs ^_^
import java.util.Scanner;
public class TempConvertor{
public static void main (String [] args){
Scanner sc = new Scanner(System.in);
System.out.println("Enter a temperature to see it's equivalent in Celsius, Fahrenheit and Kelvin");
System.out.println("If your input is in Celsius, Fahrenheit or Kelvin, press 1, 2 or 3 respectively");
double choice = sc.nextDouble();
if (choice == 1){
System.out.print("Enter the temperature in Celcius: ");
double tempC = sc.nextDouble();
double tempF = (tempC*1.8) + 32;
double tempK = tempC + 273.15;
System.out.println("Fahrenheit: " + tempF);
System.out.println("Kelvin: " + tempK);}
if (choice == 2){
System.out.print("Enter the temperature in Fahrenheit: ");
double tempF = sc.nextDouble();
double tempC = (tempF - 32)/1.8;
double tempK = tempC + 273.15;
System.out.println("Celsius: " + tempC);
System.out.println("Kelvin: " + tempK);}
if (choice == 3){
System.out.print("Enter the temperature in Kelvin: ");
double tempK = sc.nextDouble();
double tempC = tempK - 273.15;
double tempF = (tempC*1.8) + 32;
System.out.println("Celcius: " + tempC);
System.out.println("Kelvin: " + tempK);
}
if (choice != 2 && choice != 1 && choice != 3)
System.out.println("Enter only 1, 2 or 3 :(");
}
}
Output:
(i)
Enter a temperature to see it's equivalent in Celsius, Fahrenheit and Kelvin
If your input is in Celsius, Fahrenheit or Kelvin, press 1, 2 or 3 respectively
123
Enter only 1, 2 or 3 :(
(ii)
Enter a temperature to see it's equivalent in Celsius, Fahrenheit and Kelvin
If your input is in Celsius, Fahrenheit or Kelvin, press 1, 2 or 3 respectively
1
Enter the temperature in Celcius: 37
Fahrenheit: 98.60000000000001
Kelvin: 310.15
(iii)
Enter a temperature to see it's equivalent in Celsius, Fahrenheit and Kelvin
If your input is in Celsius, Fahrenheit or Kelvin, press 1, 2 or 3 respectively
2
Enter the temperature in Fahrenheit: 98.60000000000001
Celsius: 37.00000000000001
Kelvin: 310.15
(iv)
Enter a temperature to see it's equivalent in Celsius, Fahrenheit and Kelvin
If your input is in Celsius, Fahrenheit or Kelvin, press 1, 2 or 3 respectively
3
Enter the temperature in Kelvin: 310.15
Celcius: 37.0
Kelvin: 310.15