Write a C program to find the average temperature of five sunny days. Assume the temperature in Celsius.

We can do this program in two ways, one way is we can store 5 days’ temperature in 5 variables and another way is we can use an array to store 5 days’ temperature values.

Tutorial 📺

Watch tutorial and learn how to solve this problem. 😉

Steps 🪜

To Find Average Temperature of Five Sunny Days using 5 Variables

  1. Display a message to the user and accept 5 floating point values or numbers.
  2. Store these accepted values in 5 variables t1, t2, t3, t4 and t5.
  3. Now, calculate the average by adding the values in 5 variables and divide them by 5. Next, store the result in avg variable.
  4. Finally, display the result with appropriate message. That’s it. 😎

To Find Average Temperature of Five Sunny Days using Array

  1. Display a message to the user and accept 5 floating point numbers.
  2. Store these accepted values in array using loop.
  3. Now, with the help of iterative (loop) statement calculate the sum and divide it by 5 to get the average. Store the result in avg variable.
  4. Finally, display the result with appropriate message.

Flow Chart 🌻

Check how to draw flow chart for this program.

flow chart - Find the average temperature of five sunny days

Code 💻

Using Variables

Here, you can store 5 temperature values in 5 variables t1, t2, t3, t4 & t5 as shown in below program.

#include <stdio.h>
#include <conio.h>

int main()
{
  float avg=0, t1=0, t2=0, t3=0, t4=0, t5=0;
  printf("Please enter the temperature for 5 sunny days:\n");
  scanf("%f%f%f%f%f", &t1, &t2, &t3, &t4, &t5);
  avg = (t1 + t2 + t3 + t4 + t5) / 5; // find the average and store in "avg" variable
  printf("\n\nAverage temperature is: %0.2f%cC", avg, 248);
  return 0;
}

Using Array

Here, you can store 5 temperature values in an array ‘t‘ of size 5.

#include <stdio.h>
#include <conio.h>

int main()
{
  float avg=0, t[5] = {0};
  int i = 0;
  for(i ; i < 5; i++)
  {
    printf("Please enter the temperature of day%d: ", i+1);
    scanf("%f", &t[i]);
    avg += t[i]; // you can also declare a variable "sum" with initial value 0 and use it here, instead of avg.
  }
  avg /= 5; // find the average and store in "avg" variable
  printf("\n\nAverage temperature is: %0.2f%cC", avg, 248);
  return 0;
}

Note: 248 is the ASCII value of ° (degree) symbol.

Example 😍

Let’s assume, the user enters 5 floating point values which are: 12.1, 22.5, 19, 26 and 29.2

If you have declared 5 variables then all values will be stored in variables but if you are using array then each temperature value will be stored at a particular index.

The output will be like:

Average temperature is: 21.76°C

This is how we find the average temperature of five sunny days. For more C programs, click here.

Categorized in: