140k views
5 votes
Make a program that (i) asks the user for a temperature in Fahrenheit degrees and reads the number; (ii) computes the corresponding temperature in Celsius degrees; and (iii) prints out the temperature in the Celsius scale.

User MrL
by
5.2k points

1 Answer

6 votes

Answer:

//import the Scanner class to allow for user's input

import java.util.Scanner;

//Create a class declaration with appropriate name

public class FahrenheitToCelsius {

//Begin the main method where execution starts from

public static void main(String[] args) {

//Create an object of the Scanner class to allow reading from standard

// input

Scanner input = new Scanner(System.in);

//Prompt the user to enter the temperature

System.out.println("Enter a temperature in Fahrenheit degrees");

//Read the number and store in a variable of type double

double fahrenheit = input.nextDouble();

//Convert the temperature to Celsius

double celsius = (fahrenheit - 32) * (5 / 9.0);

//Print out the result

System.out.println("The temperature in Celsius is " + celsius);

} //End of main method

} //End of class declaration

==============================================================

Sample Output 1:

>> Enter a temperature in Fahrenheit degrees

32

>> The temperature in Celsius is 0.0

==============================================================

Sample Output 2:

>> Enter a temperature in Fahrenheit degrees

78

>> The temperature in Celsius is 25.555555555555557

===============================================================

Step-by-step explanation:

The code has been written in Java and it contains comments explaining every segment of the code. Please go through the comments.

The actual lines of code are written in bold face to separate them from the comments.

Sample outputs have also been given to show how the result of the program looks like.

User Daniel Lobo
by
4.9k points