Temperature Classification using if-else
R
# Input temperature from the user temperature <- as.numeric ( readline ( "Enter the temperature in Celsius: " )) if (temperature < -10.0) { cat ( "Extremely Cold\n" ) } else if (temperature < 0.0) { cat ( "Very Cold\n" ) } else if (temperature < 10.0) { cat ( "Cold\n" ) } else if (temperature < 20.0) { cat ( "Cool\n" ) } else if (temperature < 30.0) { cat ( "Warm\n" ) } else if (temperature < 40.0) { cat ( "Hot\n" ) } else { cat ( "Extremely Hot\n" ) } |
Output
Enter the temperature in Celsius: 32
Hot
The output “Hot” is displayed because the input temperature of 32 degrees Celsius falls within the range defined by the condition temperature < 40.0, which corresponds to the “Hot” category in the temperature classification.
Temperature Classification Using R
Temperature classification using nested if-else statements is a common programming task in which you categorize temperature values into different classes based on predefined ranges. This approach is often used for decision-making processes, such as determining whether to wear warm clothes, setting thermostat levels, or assessing weather conditions. In R, nested if-else statements are a useful tool for achieving this task efficiently and accurately.
Table of Content
- Concepts related to the topic:
- Classifying the Temperature
- Classifying Temperature based on Multiple Categories
- Temperature Classification using if-else
Contact Us