-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTemperatureConverter.java
More file actions
30 lines (23 loc) · 977 Bytes
/
Copy pathTemperatureConverter.java
File metadata and controls
30 lines (23 loc) · 977 Bytes
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
import java.util.Scanner;
public class TemperatureConverter {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double temperature, converted;
char unit;
System.out.println("Temperature Converter");
System.out.print("Enter temperature value: ");
temperature = sc.nextDouble();
System.out.print("Enter unit (C for Celsius, F for Fahrenheit): ");
unit = sc.next().charAt(0);
if (unit == 'C' || unit == 'c') {
converted = (temperature * 9 / 5) + 32;
System.out.printf("%.2f Celsius = %.2f Fahrenheit%n", temperature, converted);
} else if (unit == 'F' || unit == 'f') {
converted = (temperature - 32) * 5 / 9;
System.out.printf("%.2f Fahrenheit = %.2f Celsius%n", temperature, converted);
} else {
System.out.println("Invalid unit! Please enter C or F.");
}
sc.close();
}
}