Press "Enter" to skip to content

C Program to Find Sum and Average of Three Marks of a Student

This is a simple C program to find sum and average of three numbers. In this program, we read three subjects’ marks of a student and find the total and average of the given marks.
Here we have to accept three numeric values and save into three different integer variables.

Program Code:

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
void main()
int m1,m2,m3,sum;
float average;
printf("Enter three marks of the student :");
sum = m1 + m2 + m3;
average = sum / 3.0;
printf("\n Sum of the three marks is :%d", sum);
printf("\n Average of the given marks is :%.2f", average);

Code Explanation:

Variable m1, m2, m3 accept three integer numbers. And ‘sum’ is also an integer variable which gets the sum of these three number by addition operation (+).

average = sum / 3.0;

Average is a floating point variable that can keep rational numbers because the average may a rational number; that is number with a fraction.
While finding the average we divide the sum with 3 (number of subjects). According to implicit conversion of C compiler, due to the variable type of ‘sum’ (integer) the answer will be a natural number (not a rational). So we need to give a fraction of 0 with 3, that is 3.0.

For example of the three marks are 4, 6 and 7, then the sum is 17.
If we divide the sum with 3, then the result will be 5. This is not precious.
If we divide sum with 3.0 then the result will be 5.6666. This is the correct answer.

printf(“\n Average of the given marks is :%.2f”, average);

While printing the average, we can round the answer by cutting the fractional part into 2 or 3 digits.
.2f denotes the fractional digit count of the average. So the output skip remaining 6666 except 5.67

Sample Output:

Sum and average of three marks of a student in C program

Be First to Comment

Leave a Reply