2) Write a Java program to convert Celsius to Fahrenheit and vice versa. Formula: To convert Celsius to Fahrenheit, F = 1.8 * C + 32, where F is the temperature in degrees Fahrenheit and C is the temperature in degrees Celsius.
Program:
public class TemperatureConverter {
public static void main(String[] args) {
double celsius = 25.0; // Example temperature in Celsius
double fahrenheit = 77.0; // Example temperature in Fahrenheit
// Celsius to Fahrenheit
double convertedToFahrenheit = 1.8 * celsius + 32;
System.out.println("Temperature in Celsius: " + celsius);
System.out.println("Converted to Fahrenheit: " + convertedToFahrenheit);
// Fahrenheit to Celsius
double convertedToCelsius = (fahrenheit - 32) / 1.8;
System.out.println("Temperature in Fahrenheit: " + fahrenheit);
System.out.println("Converted to Celsius: " + convertedToCelsius);
}
}
Output:
Temperature in Celsius: 25.0
Converted to Fahrenheit: 77.0
Temperature in Fahrenheit: 77.0
Converted to Celsius: 25.0
No comments:
New comments are not allowed.