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[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

  1. Interview Questions On C
  2. Switch Case in C Program to Calculate Area of Circle and Triangle
  3. C Language Program to Count the Number of Lowercase Letters in a Text File
  4. Program in C to Replace Capital C with Capital S in a File
  5. C Program to Remove White Spaces and Comments from a File
  6. Perfect Number in C Programming using All Loops
  7. Reverse a Number in C
  8. Factorial Program in C Programming
  9. LCM of Two Numbers in C Programming
  10. GCD of Two Numbers in C Programming
  11. Switch Case in C Programming
  12. C Program to Display Numbers From 1 to n Except 6 and 9
  13. C Program to Count the Characters in the String Except Space
  14. Strong Number in C Programming
  15. Swapping of Two Numbers in C
  16. The while loop in C Programming
  17. C Program to Find the Sum of Cubes of Elements in an Array
  18. C Program to Print Numbers Except Multiples of n
  19. Floyd Triangle in C Programming
  20. Operators in C with Detailed Explanation
  21. C Program to Print Multiples of 5 using do-while loop
  22. Palindrome in C using Pointers
  23. Arrays in C for Complete Beginners
  24. Insert and Delete element in Array in C using switch case
  25. Programs on Arrays in C
  26. C Program to Copy the Contents of One File into Another File
  27. C Program To Print All The Numbers In a Given Range
  28. Program to Display a String in Capital Letters
  29. C Program to Remove Zeros from a number
  30. Find the Camel Case without using Library Function
  31. C Program to Display Middle Row and Column of Matrix
  32. Remove Vowels from Even Position in a String
  33. Odd Numbers in C in a Given Range
  34. Leap Year Program in C
  35. Reverse a Number using getchar and putchar function in c
  36. Basic 50 Programs in C
  37. C Program to Display Alternate Elements of a 2d Array

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;
}

Leave a Comment