w3resource

C Exercises: Accept a temperature in centigrade and display a suitable message


13. Temperature-Based Weather Message

Write a C program to read temperature in centigrade and display a suitable message according to the temperature state below:
Temp < 0 then Freezing weather
Temp 0-10 then Very Cold weather
Temp 10-20 then Cold weather
Temp 20-30 then Normal in Temp
Temp 30-40 then Its Hot
Temp >=40 then Its Very Hot

Visual Presentation:

Accept a temperature in centigrade and display a suitable message


Sample Solution:

C Code:

#include <stdio.h>   // Include the standard input/output header file.

void main()
{
    int tmp;   // Declare a variable to store temperature.

    printf("Input days temperature : ");   // Prompt user for input.
    scanf("%d", &tmp);   // Read and store temperature.

    if (tmp < 0)   // Check if temperature is less than 0.
        printf("Freezing weather.\n");   // Print message for freezing weather.
    else if (tmp < 10)   // Check if temperature is between 0 and 10.
        printf("Very cold weather.\n");   // Print message for very cold weather.
    else if (tmp < 20)   // Check if temperature is between 10 and 20.
        printf("Cold weather.\n");   // Print message for cold weather.
    else if (tmp < 30)   // Check if temperature is between 20 and 30.
        printf("Normal in temp.\n");   // Print message for normal temperature.
    else if (tmp < 40)   // Check if temperature is between 30 and 40.
        printf("Its Hot.\n");   // Print message for hot weather.
    else   // If none of the above conditions are met.
        printf("Its very hot.\n");   // Print message for very hot weather.
}

Output:

Input days temperature : 42                                                                                   
Its very hot.

Flowchart:

Flowchart: Accept a temperature in centigrade and display a suitable message


For more Practice: Solve these Related Problems:

  • Write a C program to display weather conditions based on temperature ranges using a switch-case with range labels.
  • Write a C program to output weather messages for a series of temperature inputs stored in an array.
  • Write a C program to read temperature in centigrade and display a corresponding message with error checking for invalid input.
  • Write a C program to display a custom weather message for boundary temperature values using nested if-else statements.

Go to:


PREV : Student Marks and Division Calculation.
NEXT : Triangle Type Determination.

C Programming Code Editor:



Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.