Computer Applications
Using scanner class, write a program to input temperatures recorded in different cities in °F (Fahrenheit). Convert and print each temperature in °C (Celsius). The program terminates when user enters a non-numeric character.
Answer
import java.util.Scanner;
public class KboatTemperatureConvert
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter Temperature in Fahrenheit: ");
while (in.hasNextDouble()) {
double t = in.nextDouble();
double ct = 5 / 9.0 * (t - 32);
System.out.println("Temperature in Celsius: " + ct);
System.out.print("Enter Temperature in Fahrenheit: ");
}
}
}Variable Description Table
Program Explanation
Output
Related Questions
Distinguish between the following:
hasNext() and hasNextLine()
Differentiate between hasNextInt( ) and hasNextBoolean( )
Write a program to accept a set of 50 integers. Find and print the greatest and the smallest numbers by using scanner class method.
Write a program (using scanner class) to generate a pattern of a token in the form of a triangle or in the form of an inverted triangle depending upon the user's choice.
Sample Input/Output:
Enter your choice 1
*
* *
* * *
* * * *
* * * * *Enter your choice 2
* * * * *
* * * *
* * *
* *
*