City Temperature Identification in Java


Temperature of a city in Fahrenheit degrees is input through the keyboard. Write a program to convert this temperature into Centigrade degrees.

  • The program starts by importing the java.util.Scanner package, which allows the user to input data into the program.
  • Then, it declares two variables of float data type - fr (temperature in Fahrenheit) and cd (temperature in Celsius).
  • The program then prompts the user to enter the temperature in Fahrenheit by displaying the message "Enter the Fahrenheit degrees :". It takes the input using the Scanner object and stores it in the fr variable.
  • Next, the program calculates the cd temperature in Celsius using the formula (fr-32)*(5.0f/9.0f). This formula converts the temperature in Fahrenheit to Celsius by subtracting 32 from the Fahrenheit temperature and then multiplying the result by 5/9.
  • Finally, the program displays the cd temperature in Celsius using the System.out.println() method.

Overall, this program is a basic example of how Java can be used to perform simple mathematical operations and input/output operations, specifically for temperature conversions.


Source Code

import java.util.Scanner;
class Centigrade_Degree
{
	public static void main(String args[])
	{
		Scanner input = new Scanner(System.in);
		System.out.println("Enter the Fahrenheit degrees :");
		float fr = input.nextFloat();
		float cd = ((fr-32)*(5.0f/9.0f));		
		System.out.println("Temparture in Centigrade : "+cd);
	}
}

Output

Enter the Fahrenheit degrees :
45.67
Temparture in Centigrade : 7.594444