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[50],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
Important C Programs
- Interview Questions On C
- Switch Case in C Program to Calculate Area of Circle and Triangle
- C Language Program to Count the Number of Lowercase Letters in a Text File
- Program in C to Replace Capital C with Capital S in a File
- C Program to Remove White Spaces and Comments from a File
- Perfect Number in C Programming using All Loops
- Reverse a Number in C
- Factorial Program in C Programming
- LCM of Two Numbers in C Programming
- GCD of Two Numbers in C Programming
- Switch Case in C Programming
- C Program to Display Numbers From 1 to n Except 6 and 9
- C Program to Count the Characters in the String Except Space
- Strong Number in C Programming
- Swapping of Two Numbers in C
- The while loop in C Programming
- C Program to Find the Sum of Cubes of Elements in an Array
- C Program to Print Numbers Except Multiples of n
- Floyd Triangle in C Programming
- Operators in C with Detailed Explanation
- C Program to Print Multiples of 5 using do-while loop
- Palindrome in C using Pointers
- Arrays in C for Complete Beginners
- Insert and Delete element in Array in C using switch case
- Programs on Arrays in C
- C Program to Copy the Contents of One File into Another File
- C Program To Print All The Numbers In a Given Range
- Program to Display a String in Capital Letters
- C Program to Remove Zeros from a number
- Find the Camel Case without using Library Function
- C Program to Display Middle Row and Column of Matrix
- Remove Vowels from Even Position in a String
- Odd Numbers in C in a Given Range
- Leap Year Program in C
- Reverse a Number using getchar and putchar function in c
- Basic 50 Programs in C
- C Program to Display Alternate Elements of a 2d Array
FAQs
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;
}
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;
}