Here is a C++ program to find average marks of five subjects using loop. In this C++ program, we will calculate the Total Marks, Average and Percentage Marks of N subjects entered by user. We will first ask user to enter the number of subjects and then the marks of individual subjects. Then we will print Total marks of all subjects, average and percentage marks of all subjects on screen. Here is the formulae to calculate Total Average and percentage marks.
Let Number of subjects be N and each subject is of 100 marks.Total_Marks : Sum of marks of all Subjects.
Average_Marks = Total_Marks/N.
Percentage = (Total_Marks/(N x 100)) x 100;
C++ Program to find Total, Average and Percentage Marks
#include <iostream> using namespace std; int main(){ int subjects, i; float marks, total=0.0f, averageMarks, percentage; // Input number of subjects cout << "Enter number of subjects\n"; cin >> subjects; // Take marks of subjects as input cout << "Enter marks of subjects\n"; for(i = 0; i < subjects; i++){ cin >> marks; total += marks; } // Calculate Average averageMarks = total / subjects; // Each subject is of 100 Marks percentage = (total/(subjects * 100)) * 100; cout << "Total Marks = " << total; cout << "\nAverage Marks = " << averageMarks; cout << "\nPercentage = " << percentage; return 0; }Output
Enter number of subjects 5 Enter marks of subjects 34 65 96 47 62 Total Marks = 304 Average Marks = 60.8 Percentage = 60.8
In above program, we first ask user to enter the number of subjects and store it in variable "subjects". Then using a for loop, we take the marks of each subject as input from user and add it to variable "total". After for loop, we will have total marks of all subjects in variable "total".
Then we calculate the value of average marks and percentage marks as per expression given above. Finally, we print the value of Total Marks, Average Marks and Percentage Marks on screen using cout.
Recommended Posts