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

Online Exam Quiz for One day Exam Online Typing Test CCC Online Test 2026 Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level Online Test in Hindi Best Website and Software Company in Allahabad