Program To Calculate The Average Marks of 5 Subjects

Posted in

Program To Calculate The Average Marks of 5 Subjects
vinaykhatri

Vinay Khatri
Last updated on November 9, 2024

    Problem Statement

    Rahul is a student in class 11th and he has 5 subjects, and we need to write a script that accepts Rahul's all 5 subjects marks and calculate the average. In this tutorial, we will learn 2 different approaches to store the marks of 5 different subjects and calculate their average.

    Approach 1 (Use Identifier):

    In our first approach, we will use 5 different identifiers or variables, to store the marks (value) of every subject. And to calculate the average of all the marks sum up all the variable's values and divide them by 5.

    C Program to calculate the average marks of 5 subjects

    #include <stdio.h>
    int main()
    {
    	//initialize variables for each subject
    	int math, phy, chem, eng,cs; 
    	
    	float avg; 
    	//ask user to enter the marks for individual subject
    	printf("Enter math marks: "); 
    	scanf("%d", &math);
    	 
    	printf("Enter physics marks: "); 
    	scanf("%d", &phy); 
    	
    	printf("Enter chemistry marks: "); 
    	scanf("%d",&chem); 
    	printf("Enter english marks: "); 
    	scanf("%d",&eng); 
    	
    	printf("Enter cs marks: " ); 
    	scanf("%d",&cs); 
    	
    	//calculate the average of the total marks
    	avg = (math+phy+chem+eng+cs)/5; 
    	
    	printf("The average marks you got is: %.2f",avg);
    
        return 0;
    }

    Output

    Enter math marks: 97
    Enter physics marks: 96
    Enter chemistry marks: 93
    Enter english marks: 89
    Enter cs marks: 90
    The average marks you got is: 93.00

    C++Program to calculate the average marks of 5 subjects:

    #include<iostream.h>
    #include< conio.h>
    
    void main()
    {
    clrscr();
    int math, phy, chem, eng,cs;
    float avg;
    cout<<"Enter math marks: ";
    cin>>math;
    cout<<"Enter physics marks: ";
    cin>>phy;
    cout<<"Enter chemistry marks: ";
    cin>>chem;
    cout<<"Enter english marks: ";
    cin>>eng;
    cout<<"Enter cs marks: " ;
    cin>>cs;
    avg = (math+phy+chem+eng+cs)/5;
    cout<<"The average marks you got is: "<<avg;
    getch();
    }

    Output

    Enter math marks: 96
    Enter physics marks: 87
    Enter chemistry marks: 95
    Enter english marks: 95
    Enter cs marks: 90
    The average marks you got is: 92.6

    Python Program to calculate the average marks of 5 subjects

    math =int(input("Enter math marks: "))
    phy =int(input("Enter physics marks: "))
    chem =int(input("Enter chemistry marks: "))
    eng =int(input("Enter english marks: "))
    cs =int(input("Enter cs marks: ")) 
    avg = (math+phy+chem+eng+cs)/5
    print("The average marks you got is:",avg)

    Output

    Enter math marks: 96
    Enter physics marks: 87
    Enter chemistry marks: 95
    Enter english marks: 95
    Enter cs marks: 90
    The average marks you got is: 92.6

    Approach 2

    In the above approach, we used 5 different identifiers to store 5 different marks values. When we encounter such problems where we have a similar type of data to store and compute. There instead of using different identifiers or variable names we should always use arrays. Now let's write the program that will give us the same result as above, but instead of 5 identifiers, now we will only use a single array identifier marks .

    C Program to calculate the average marks of 5 subjects using an array

    #include <stdio.h>
    int main()
    {
    	//initialize an array that will store marks of 5 subjects
    	int marks[5];
    	int i;
    	float total=0,avg;
    	 
    	//ask user to enter the marks for individual subject
    	printf("Enter math marks: "); 
    	scanf("%d", &marks[0]);
    	 
    	printf("Enter physics marks: "); 
    	scanf("%d", &marks[1]); 
    	
    	printf("Enter chemistry marks: "); 
    	scanf("%d",&marks[2]);
    	 
    	printf("Enter english marks: "); 
    	scanf("%d",&marks[3]); 
    	
    	printf("Enter cs marks: " ); 
    	scanf("%d",&marks[4]); 
    	
    	//calculate the total marks
    	for(i=0; i<5; i++)
    	{
    		total+=marks[i];
    	}
    	
    	//compute the average of 5 subjects
    	avg = total/5;
    	
    	printf("The average marks you got is: %.2f",avg);
    
        return 0;
    }

    Output

    Enter math marks: 98
    Enter physics marks: 97
    Enter chemistry marks: 96
    Enter english marks: 95
    Enter cs marks: 94
    The average marks you got is: 96.00

    C++ Program To Calculate The Average Marks of 5 subjects using an array

    #include <iostream>
    using namespace std;
    
    int main()
    {
    	//initialize an array that will store marks of 5 subjects
    	int marks[5];
    	int i;
    	float total=0,avg;
    	 
    	//ask user to enter the marks for individual subject
    	cout<<"Enter math marks: "; cin>>marks[0];
    	 
    	cout<<"Enter physics marks: "; cin>>marks[1]; 
    	
    	cout<<"Enter chemistry marks: "; cin>>marks[2];
    	 
    	cout<<"Enter english marks: "; cin>>marks[3]; 
    	
    	cout<<"Enter cs marks: " ; cin>>marks[4]; 
    	
    	//calculate the total marks
    	for(i=0; i<5; i++)
    	{
    		total+=marks[i];
    	}
    	
    	//compute the average of 5 subjects
    	avg = total/5;
    	
    	cout<<"The average marks you got is: "<<avg;
    
        return 0;
    }

    Output

    Enter math marks: 98
    Enter physics marks: 97
    Enter chemistry marks: 96
    Enter english marks: 95
    Enter cs marks: 94
    The average marks you got is: 96

    Python Program To Calculate The Average Marks of 5 subjects using an array

    # initialize an array that will store marks of 5 subjects
    marks =[]
    
    # ask user to enter the marks for individual subject
    marks.append(int(input("Enter math marks: ")))
    marks.append(int(input("Enter physics marks: ")))
    marks.append(int(input("Enter chemistry marks: ")))
    marks.append(int(input("Enter english marks: ")))
    marks.append(int(input("Enter cs marks: ")))
    
    # calculate the total marks
    total = sum(marks)
    
    # calculate average
    avg = total/5
    
    print(f"The average marks you got is: {round(avg,2)}")

    Output

    Enter math marks: 98
    Enter physics marks: 97
    Enter chemistry marks: 96
    Enter english marks: 95
    Enter cs marks: 94
    The average marks you got is: 96.0

    Wrapping Up!

    In this programming tutorial, we learned how can we store marks of 5 subjects and calculate the average marks. In our first approach, we basically follow the naive approach where we assign individual variable names or identifiers for every subject. But in the second approach, we used an array that provides a more optimal and efficient approach to solve the problem. Although in a programming interview you may not find such easy questions, but if you find a similar one always try to use an array to store such data values that are similar and have the same data type.

    People are also reading:

    Leave a Comment on this Post

    0 Comments