w3resource

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

C Conditional Statement: Exercise-13 with Solution

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.
}

Sample Output:

Input days temperature : 42                                                                                   
Its very hot.

Flowchart:

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

C Programming Code Editor:

Previous: Write a C program to read roll no, name and marks of three subjects and calculate the total, percentage and division.
Next: Write a C program to check whether a triangle is Equilateral, Isosceles or Scalene.

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.