-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP02_CelsiusToFahrenheit.java
More file actions
38 lines (32 loc) · 1.33 KB
/
Copy pathP02_CelsiusToFahrenheit.java
File metadata and controls
38 lines (32 loc) · 1.33 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
package programs;
/**
* ============================================================
* PROGRAM 02: Celsius to Fahrenheit & Vice Versa Converter
* ============================================================
* Problem: WAP to convert temperature:
* - From Celsius to Fahrenheit: F = (C * 9/5) + 32
* - From Fahrenheit to Celsius: C = (F - 32) * 5/9
* ============================================================
*/
public class P02_CelsiusToFahrenheit {
public static double celsiusToFahrenheit(double celsius) {
return (celsius * 9.0 / 5.0) + 32.0;
}
public static double fahrenheitToCelsius(double fahrenheit) {
return (fahrenheit - 32.0) * (5.0 / 9.0);
}
public static void main(String[] args) {
double[] testCelsius = {0.0, 25.0, 37.0, 100.0, -40.0};
System.out.println("=== CELSIUS TO FAHRENHEIT ===");
for (double c : testCelsius) {
double f = celsiusToFahrenheit(c);
System.out.printf(" %6.1f °C = %6.1f °F%n", c, f);
}
System.out.println("\n=== FAHRENHEIT TO CELSIUS (REVERSE) ===");
double[] testFahrenheit = {32.0, 77.0, 98.6, 212.0, -40.0};
for (double f : testFahrenheit) {
double c = fahrenheitToCelsius(f);
System.out.printf(" %6.1f °F = %6.1f °C%n", f, c);
}
}
}