-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTemperatureConverter.java
36 lines (27 loc) · 1.22 KB
/
TemperatureConverter.java
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
import java.util.Scanner;
class TemperatureConverter {
// Main method
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Convert Celsius to Fahrenheit");
System.out.print("Enter the temperature in Celsius: ");
double celsius = input.nextDouble();
// Method to convert from celsius to fahrenheit
double fahrenheitTemp = celsiusToFahrenheit(celsius);
System.out.printf("Temperature in Fahrenheit: %.2f\n" , fahrenheitTemp);
System.out.println("Convert Fahrenheit to Celsius");
System.out.print("Enter the temperature in Fahrenheit: ");
double fahrenheit = input.nextDouble();
// Method to convert from fahrenheit to celsius
double celsiusTemp = fahrenheitToCelsius(fahrenheit);
System.out.printf("Temperature in Celsius: %.2f\n" , celsiusTemp);
}
// Method to convert from celsius to fahrenheit
public static double celsiusToFahrenheit(double celsius) {
return (celsius * 9/5) + 32;
}
// Method to convert from fahrenheit to celsius
public static double fahrenheitToCelsius(double fahrenheit) {
return (fahrenheit - 32) * 5/9;
}
}