This repository was archived by the owner on Jun 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathTemperatureConverter.java
More file actions
85 lines (75 loc) · 3.41 KB
/
TemperatureConverter.java
File metadata and controls
85 lines (75 loc) · 3.41 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import java.util.Scanner;
public class TemperatureConverter {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("Temperature Converter");
System.out.println("1. Celsius to Fahrenheit");
System.out.println("2. Fahrenheit to Celsius");
System.out.println("3. Celsius to Kelvin");
System.out.println("4. Kelvin to Celsius");
System.out.println("5. Fahrenheit to Kelvin");
System.out.println("6. Kelvin to Fahrenheit");
System.out.println("7. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
double temperature;
switch (choice) {
case 1:
System.out.print("Enter temperature in Celsius: ");
temperature = scanner.nextDouble();
System.out.println("Temperature in Fahrenheit: " + celsiusToFahrenheit(temperature));
break;
case 2:
System.out.print("Enter temperature in Fahrenheit: ");
temperature = scanner.nextDouble();
System.out.println("Temperature in Celsius: " + fahrenheitToCelsius(temperature));
break;
case 3:
System.out.print("Enter temperature in Celsius: ");
temperature = scanner.nextDouble();
System.out.println("Temperature in Kelvin: " + celsiusToKelvin(temperature));
break;
case 4:
System.out.print("Enter temperature in Kelvin: ");
temperature = scanner.nextDouble();
System.out.println("Temperature in Celsius: " + kelvinToCelsius(temperature));
break;
case 5:
System.out.print("Enter temperature in Fahrenheit: ");
temperature = scanner.nextDouble();
System.out.println("Temperature in Kelvin: " + fahrenheitToKelvin(temperature));
break;
case 6:
System.out.print("Enter temperature in Kelvin: ");
temperature = scanner.nextDouble();
System.out.println("Temperature in Fahrenheit: " + kelvinToFahrenheit(temperature));
break;
case 7:
System.out.println("Exiting...");
scanner.close();
System.exit(0);
default:
System.out.println("Invalid choice");
}
}
}
public static double celsiusToFahrenheit(double celsius) {
return (celsius * 9 / 5) + 32;
}
public static double fahrenheitToCelsius(double fahrenheit) {
return (fahrenheit - 32) * 5 / 9;
}
public static double celsiusToKelvin(double celsius) {
return celsius + 273.15;
}
public static double kelvinToCelsius(double kelvin) {
return kelvin - 273.15;
}
public static double fahrenheitToKelvin(double fahrenheit) {
return (fahrenheit - 32) * 5 / 9 + 273.15;
}
public static double kelvinToFahrenheit(double kelvin) {
return (kelvin - 273.15) * 9 / 5 + 32;
}
}