The problem
Write a program that reads a Celsius degree in a double value from the console, then converts it to Fahrenheit and displays the result. The formula to do so is:
fahrenheit = (9 / 5) * celsius + 32
Hint:In Java, 9 / 5 is 1, but 9.0 / 5 is 1.8. Here is a sample run:
Enter a degree in Celsius: 43
43 Celsius is 109.4 Fahrenheit
Breaking it down
First displaying message for a user to enter a number then setting users input to celsius
for the calculation. One thing to be aware of is a user could enter something other than a number so should look to validate the input and provide a message back to the user. Calling the convertCelsiusToFahrenheit
and passing celsius
will return the calculation needed to display back to the user.
public static void main(String[] Strings) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a degree in Celsius: ");
double celsius = input.nextDouble();
input.close();
double fahrenheit = convertCelsiusToFahrenheit(celsius);
System.out.println(celsius + " degree Celsius is equal to "
- fahrenheit + " in Fahrenheit");
}
private static double convertCelsiusToFahrenheit(double celsius) {
double fahrenheit = (9.0 / 5.0) * celsius + 32.0;
return fahrenheit;
}
Output
Enter a degree in Celsius: 12
12.0 Celsius is 53.6 in Fahrenheit