# C Program to Calculate Average Marks Scored by a Student for 3 Subjects

## Introduction

In this post, we are going to write a c program to calculate average marks scored by a student for 3 subjects. Here, we are writing this program only for a single student. We will read marks of 3 subjects and then calculate the average.

### What is an average?

Average is nothing but a sum of numbers divided by total numbers. For example, average of 2 and 3 is 2.5. How we have calculated?

Average = (2 + 3) / 2

See the following expected output.

``````Enter marks for 3 subjects
57
85
88
Average Marks of 57, 85 and 88 is 76.67``````

Now, we are going to write the same program which will display the above output.

### C Program to Calculate Average Marks Scored by a Student for 3 Subjects

``````#include <stdio.h>
#include <stdlib.h>
int main()
{
int m1,m2,m3;
float avg;
printf("Enter marks for 3 subjects\n");
scanf("%d%d%d",&m1,&m2,&m3);
if(((m1>=0)&&(m1<=100))&&(((m2>=0)&&(m2<=100)))&&((m3>=0)&&(m3<=100)))
{
avg=(m1+m2+m3)/3.0;
printf("Average marks of %d, %d and %d is %0.2f",m1,m2,m3,avg);
}
else
{
printf("Marks must be in between 0 and 100");
}

return 0;
}
``````

Here, marks of each subject must be in between 0 to 100, therefore, I have written the if statement. In this statement, we are checking whether the user has entered the marks in between 0 to 100. If not, then we will not calculate the average.

I hope, you have understood this program. If you have any doubt or any feedback, then please feel free to contact me.

## Some Other C Programs on Average

Here, I am writing some other c programs on average.

### Average of two numbers in C

``````#include <stdio.h>
#include <stdlib.h>
int main()
{
int num1,num2;
float avg;
printf("Enter any two numbers\n");
scanf("%d%d",&num1,&num2);
avg=(num1+num2)/2.0;
printf("Average of %d and %d is %0.2f",num1,num2,avg);
return 0;
}``````

#### Ouput

``````Enter any two numbers
5
7
Average of 5 and 7 is 6.00``````

### C Program to Find Average of n Numbers using for loop

``````#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,n,num,sum=0;
float avg;
printf("How many numbers are there?\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Enter number %d\n",i);
scanf("%d",&num);
sum=sum+num;
}
avg=sum/(float)n;
printf("Average = %0.2f",avg);
return 0;
}
``````

In this program, we are asking the user to enter total elements. Using for loop, we are finding the average of n numbers. See the following output.

#### Output

``````How many numbers are there?
5
Enter number 1
1
Enter number 2
2
Enter number 3
3
Enter number 4
4
Enter number 5
5
Average = 3.00``````

### C Program to Find Average of n Numbers using Array

``````#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,n,num,sum=0;
float avg;
printf("Enter the size of an Array\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter number %d\n",i+1);
scanf("%d",&num[i]);
sum=sum+num[i];
}
printf("Given Array: ");
for(i=0;i<n;i++)
{
printf("%d ",num[i]);
}
avg=sum/(float)n;
printf("\nAverage = %0.2f",avg);
return 0;
}
``````

#### Output

``````Enter the size of an Array
5
Enter number 1
1
Enter number 2
2
Enter number 3
3
Enter number 4
4
Enter number 5
5
Given Array: 1 2 3 4 5
Average = 3.00``````

### C Program to Find Average of n Numbers Using Functions

``````#include <stdio.h>
#include <stdlib.h>
int main()
{
float average(int);
int n;
float avg;
printf("How many numbers are there?\n");
scanf("%d",&n);
avg=average(n);
printf("Average = %0.2f",avg);
return 0;
}
float average(int total)
{
int i,num,sum=0;
float ag;
for(i=1;i<=total;i++)
{
printf("Enter number %d\n",i);
scanf("%d",&num);
sum=sum+num;
}
ag=sum/(float)total;
return ag;
}
``````

#### Output

``````How many numbers are there?
5
Enter number 1
1
Enter number 2
2
Enter number 3
3
Enter number 4
4
Enter number 5
5
Average = 3.00``````

## FAQs

C Program to Find Average of 3 Numbers

int main()
{
int n1,n2,n3,n;
float avg;
printf(“Enter any three numbers\n”);
scanf(“%d%d%d”,&n1,&n2,&n3);
avg=(n1+n2+n3)/3.0;
printf(“Average of %d, %d and %d is %0.2f”,n1,n2,n3,avg);
return 0;
}

C Program to Find Average of 3 Numbers using Function

int main()
{
float average(int,int,int);
int n1,n2,n3,n;
float avg;
printf(“Enter any three numbers\n”);
scanf(“%d%d%d”,&n1,&n2,&n3);
avg=average(n1,n2,n3);
printf(“Average of %d, %d and %d is %0.2f”,n1,n2,n3,avg);
return 0;
}
float average(int a,int b, int c)
{
float ag;
ag=(n1+n2+n3)/3.0;
return ag;
}