-
Notifications
You must be signed in to change notification settings - Fork 0
/
Temperature Conversion
60 lines (51 loc) · 1.88 KB
/
Temperature Conversion
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import java.util.Scanner;
public class TemperatureConversion
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the temperature value: ");
double temperature = scanner.nextDouble();
System.out.println("Enter the unit of the temperature (C for Celsius, F for Fahrenheit, K for Kelvin): ");
char unit = scanner.next().toUpperCase().charAt(0);
switch (unit)
{
case 'C':
convertFromCelsius(temperature);
break;
case 'F':
convertFromFahrenheit(temperature);
break;
case 'K':
convertFromKelvin(temperature);
break;
default:
System.out.println("Invalid unit entered.");
}
scanner.close();
}
public static void convertFromCelsius(double celsius)
{
double fahrenheit = (celsius * 9/5) + 32;
double kelvin = celsius + 273.15;
System.out.println("Celsius: " + celsius + "°C");
System.out.println("Fahrenheit: " + fahrenheit + "°F");
System.out.println("Kelvin: " + kelvin + "K");
}
public static void convertFromFahrenheit(double fahrenheit)
{
double celsius = (fahrenheit - 32) * 5/9;
double kelvin = (fahrenheit - 32) * 5/9 + 273.15;
System.out.println("Fahrenheit: " + fahrenheit + "°F");
System.out.println("Celsius: " + celsius + "°C");
System.out.println("Kelvin: " + kelvin + "K");
}
public static void convertFromKelvin(double kelvin)
{
double celsius = kelvin - 273.15;
double fahrenheit = (kelvin - 273.15) * 9/5 + 32;
System.out.println("Kelvin: " + kelvin + "K");
System.out.println("Celsius: " + celsius + "°C");
System.out.println("Fahrenheit: " + fahrenheit + "°F");
}
}