The Program in C that gets temperatures of the week & calculate the average temperature for that week is given below:
#include <stdio.h>
int main() {
float temp, sum = 0, avg;
for (int i = 1; i <= 7; i++) {
printf("Enter the temperature for day %d: ", i);
scanf("%f", &temp);
sum += temp;
}
avg = sum / 7.0;
printf("The average temperature for the week is: %.2f", avg);
return 0;
}
Output:
Enter the temperature for day 1: 32
Enter the temperature for day 2: 34
Enter the temperature for day 3: 49
Enter the temperature for day 4: 33
Enter the temperature for day 5: 36
Enter the temperature for day 6: 32
Enter the temperature for day 7: 20
The average temperature for the week is: 32.43
Pro-Tips💡
This program uses a for loop to prompt the user to enter the temperature for each day of the week. The variable temp
stores the temperature entered by the user, and sum
keeps track of the total temperature for the week.
The loop iterates 7 times, one for each day of the week. Inside the loop, the temperature for that day is added to the sum
variable.
After the loop, the average temperature for the week is calculated by dividing the total temperature by 7(number of days in a week).
The result is then printed with 2 decimal points precision.
Learn C-Sharp ↗
C-sharp covers every topic to learn about C-Sharp thoroughly.
Learn C Programming ↗
C-Programming covers every topic to learn about C-Sharp thoroughly.
Learn C++ Programming↗
C++ covers every topic to learn about C-Sharp thoroughly.