-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP3_Temperature_Converter.java
More file actions
60 lines (55 loc) · 2.45 KB
/
P3_Temperature_Converter.java
File metadata and controls
60 lines (55 loc) · 2.45 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
import java.util.Scanner;
public class P3_Temperature_Converter {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int choice;
do {
System.out.println("\nTemperature Converter");
System.out.println("1. Celsius to Fahrenheit");
System.out.println("2. Celsius to Kelvin");
System.out.println("3. Fahrenheit to Celsius");
System.out.println("4. Fahrenheit to Kelvin");
System.out.println("5. Kelvin to Celsius");
System.out.println("6. Kelvin to Fahrenheit");
System.out.println("7. Exit");
System.out.print("Enter your choice: ");
choice = scanner.nextInt();
if (choice >= 1 && choice <= 6) {
System.out.print("Enter temperature value: ");
double temp = scanner.nextDouble();
double convertedTemp = 0;
if (choice == 1) {
convertedTemp = (temp * 9 / 5) + 32;
System.out.println("Converted Temperature: " + convertedTemp + " °F");
}
else if (choice == 2) {
convertedTemp = temp + 273.15;
System.out.println("Converted Temperature: " + convertedTemp + " K");
}
else if (choice == 3) {
convertedTemp = (temp - 32) * 5 / 9;
System.out.println("Converted Temperature: " + convertedTemp + " °C");
}
else if (choice == 4) {
convertedTemp = (temp - 32) * 5 / 9 + 273.15;
System.out.println("Converted Temperature: " + convertedTemp + " K");
}
else if (choice == 5) {
convertedTemp = temp - 273.15;
System.out.println("Converted Temperature: " + convertedTemp + " °C");
}
else {
convertedTemp = (temp - 273.15) * 9 / 5 + 32;
System.out.println("Converted Temperature: " + convertedTemp + " °F");
}
}
else if (choice == 7) {
System.out.println("Exiting... Thank you for using the Temperature Converter.");
}
else {
System.out.println("Invalid choice! Please try again.");
}
} while (choice != 7);
scanner.close();
}
}