Write a C program to get the marks of 5 subjects and find the total and average
Description: Take the marks of 5 subjects as input. Print the total and the average.
Hint:
- Average is
total / 5. Usefloatso the decimal part is not lost.
C program to find total and average marks
#include <stdio.h>
int main(void) {
int m1, m2, m3, m4, m5, total;
float average;
printf("Enter marks of 5 subjects: ");
scanf("%d %d %d %d %d", &m1, &m2, &m3, &m4, &m5);
total = m1 + m2 + m3 + m4 + m5;
average = total / 5.0f;
printf("Total = %d\n", total);
printf("Average = %.2f\n", average);
return 0;
}
Output:
Output of the above program is
Enter marks of 5 subjects: 70 65 80 90 55
Total = 360
Average = 72.00
How it works
- The 5 values are read with one
scanf(). - We divide by
5.0f(a float), so the result keeps its decimal part. With5the division would be an integer division.
Read C Type Casting.
Learn more about the similar topics:
| Tutorials |
|---|
| No Content Found. |
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |