Programming Examples
Java program to find temperature category using Ternary Operator
Write a Java program that takes a temperature value as input and categorizes it as "Cold" if the temperature is below 10 degrees, "Warm" if it's between 10 and 30 degrees, and "Hot" if it's above 30 degrees using the ternary operator.
Solution
import java.util.Scanner;
public class TemperatureDescription
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the temperature: ");
int temperature = sc.nextInt();
String description = (temperature < 10) ? "Cold" :
(temperature <= 30) ? "Warm" : "Hot";
System.out.println("The temperature is: " + description);
}
}
Output