Average Marks Scored by Student for 3 Subjects in C++

Program description:- Write a C++ program to calculate average marks scored by a student for 3 subjects. Take the marks for those three subjects as input from the end-user, calculate average value and display them.

To calculate the average value of marks scored by the student first we need to calculate the total marks. Then the average value can be calculated as total marks/3.

Assume n1, n2, and n3 are the marks of three subject scored by a student. Then the total marks can be calculated as (n1+n2+n3) and average mark can be calculated as (total marks/3).

Total Mark = n1 + n2 + n3
Average Mark = total_mark / 3

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

#include<iostream>
using namespace std;
int main()
{
   // declare variables
   int n1, n2, n3;
   int total;
   double avg;

   // take input for marks of 3 sub
   cout << "Enter Marks of 3 Subjects: ";
   cin >> n1 >> n2 >> n3;
  
   // calculate total marks
   total = n1 + n2 + n3;

   // calculate average mark
   avg = total/3;
  
   // display result
   cout << "Total Mark = " << total << endl;
   cout << "Average Mark = " << avg << endl;

   return 0;
}

Output:-

Enter Marks of 3 Subjects: 75 81 84
Total Mark = 240
Average Mark = 80

Enter Marks of 3 Subjects: 49 55 45
Total Mark = 149
Average Mark = 49

The mark for a subject will be an integer number, not the floating-point number therefore we took int data type to store the input value entered by the end-user. The total marks also will be an integer number but the average value can be an integer number or floating-point number therefore we took int data type for the total marks and double data type for the average marks.

To take input from the end-user we have used cin and to display output message to the console window, we used cout.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *