Exercise
Conversion
Objetive
Write a java program to convert Celsius degrees to Kelvin and Fahrenheit. The program will prompt the user to input the temperature in Celsius degrees, and then use the following conversion formulas:
Kelvin = Celsius + 273
Fahrenheit = Celsius x 1.8 + 32
The program will then display the equivalent temperature in both Kelvin and Fahrenheit units.
Example Code
// Importing the Scanner class to handle input from the useimport java.util.Scanner; // Import Scanner class for input
public class Program {
// Main entry point of the program
public static void main(String[] args) {
// Create a scanner object to read user input
Scanner scanner = new Scanner(System.in);
// Ask the user to input the temperature in Celsius
System.out.print("Enter the temperature in Celsius: ");
// Read the user's input as a double
double celsius = scanner.nextDouble();
// Convert Celsius to Kelvin using the formula
double kelvin = celsius + 273;
// Convert Celsius to Fahrenheit using the formula
double fahrenheit = celsius * 1.8 + 32;
// Display the equivalent temperature in Kelvin
System.out.println("Temperature in Kelvin: " + kelvin);
// Display the equivalent temperature in Fahrenheit
System.out.println("Temperature in Fahrenheit: " + fahrenheit);
// Close the scanner to free up resources
scanner.close();
}
}