C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Structures and Union in C

Store weather report data

C Program: Store weather report data

C

#include <stdio.h>

 

struct Weather {

    int day;

    float temperature;

    float humidity;

    float rainfall;

};

 

int main() {

    struct Weather report[31];

    int n, i;

 

    printf("Enter number of days for weather report (max 31): ");

    scanf("%d", &n);

 

    // Input weather data

    for (i = 0; i < n; i++) {

        printf("\nEnter data for Day %d\n", i + 1);

 

        report[i].day = i + 1;

 

        printf("Temperature (°C): ");

        scanf("%f", &report[i].temperature);

 

        printf("Humidity (%%): ");

        scanf("%f", &report[i].humidity);

 

        printf("Rainfall (mm): ");

        scanf("%f", &report[i].rainfall);

    }

 

    // Display weather report

    printf("\n==================== WEATHER REPORT ====================\n");

    printf("%-10s %-15s %-15s %-15s\n", "Day", "Temperature(°C)", "Humidity(%)", "Rainfall(mm)");

    printf("--------------------------------------------------------\n");

 

    for (i = 0; i < n; i++) {

        printf("%-10d %-15.2f %-15.2f %-15.2f\n",

               report[i].day, report[i].temperature,

               report[i].humidity, report[i].rainfall);

    }

 

    printf("========================================================\n");

 

    return 0;

}

Output

 
OUTPUT :

Enter number of days for weather report (max 31): 3

Enter data for Day 1
Temperature (°C): 28.5
Humidity (%): 60
Rainfall (mm): 5.2

Enter data for Day 2
Temperature (°C): 31.0
Humidity (%): 55
Rainfall (mm): 0.0

Enter data for Day 3
Temperature (°C): 27.3
Humidity (%): 70
Rainfall (mm): 12.5

==================== WEATHER REPORT ================
Day        Temperature(°C)      Humidity(%)        Rainfall(mm)
---------------------------------------------------------------------
1               28.50                        60.00              5.20
2               31.00                        55.00              0.00
3               27.30                        70.00             12.50
=================================================

Explanation

Concept

Description

struct Weather

Defines a structure to store data for each day — temperature, humidity, and rainfall.

report[31]

Array to hold weather data for up to 31 days.

for loop

Used for both input and output of daily weather records.

printf() formatting

Used to align the output like a table.