Display marksheet using function in C++


Example Program : 1

This code is a simple program that calculates the total marks and average of marks obtained by a student in five subjects. It also determines the grade and whether the student passed or failed based on the total marks obtained.

The program defines a function marks() which takes input for marks in five subjects and calculates the total and average marks. It then uses a series of if-else statements to determine the grade and whether the student passed or failed based on the total marks. The program is then called from the main() function and exits after the execution of marks().

Overall, the program is a good example of using simple input/output statements and if-else statements to perform basic calculations and conditionals. However, it could be improved by using more meaningful variable names and by implementing error handling for invalid input values.

Source Code

#include<iostream>
using namespace std;
void marks();
int main()
{
    marks();
    return 0;
}
void marks()
{
    int tam,eng,mat,sci,soc,tot,avg;
    cout<<"\nEnter the Marks:";
    cin>>tam>>eng>>mat>>sci>>soc;
    tot=tam+eng+mat+sci+soc;
    avg=tot/5;
    cout<<"\nTotal Marks:"<<tot;
    cout<<"\nAverage:"<<avg;
    if(tot>230)
    {
        if(tot>=480)
        {
            cout<<"\nGrade:A";
        }
        else if(tot>400&&tot<480)
        {
            cout<<"\nGrade:B";
        }
        else if(tot<400)
        {
            cout<<"\nGrade:C";
        }
        cout<<"\nPass";
    }
    else
    {
        cout<<"\nFail";
    }
}
To download raw file Click Here

Output

Enter the Marks:78
65
98
86
78
Total Marks:405
Average:81
Grade:B
Pass

Example Program : 2

The program prompts the user to input the marks for five subjects (Tamil, English, Maths, Science and Social Science) and calculates the total marks and average marks. It then calls the function marklist() with the marks as arguments.

The marklist() function takes five arguments representing the marks for each subject and calculates the total marks and average marks again. It then checks if the total marks are greater than 250, which is the minimum passing mark. If the total marks are greater than 250, it prints "Pass". Otherwise, it prints "Fail". The program doesn't calculate grades based on the total marks.

Source Code

#include<iostream>
using namespace std;
void marklist(int,int,int,int,int);
int main()
{
    int tam,eng,mat,sci,soc,avg;
    cout<<"\nEnter the Marks:";
    cin>>tam>>eng>>mat>>sci>>soc;
    marklist(tam,eng,mat,sci,soc);
    return 0;
}
void marklist(int tam,int eng,int mat,int sci,int soc)
{
    int avg,tot;
    tot=tam+eng+mat+sci+soc;
    cout<<"\nTotal marks:"<<tot;
    avg=tot/5;
    cout<<"\nAverage mark:"<<avg;
    if(tot>250)
    {
        cout<<"\nPass";
    }
    else
    {
        cout<<"\nFail";
    }
}
To download raw file Click Here

Output

Enter the Marks:89
87
87
67
89
Total marks:419
Average mark:83
Pass

Example Program : 3

This code defines a function marklist that takes five integer parameters (tam, eng, mat, sci, and soc) representing marks in five subjects and returns the total marks. In main, the user inputs marks in five subjects, and the marklist function is called with these inputs. The total marks returned by the function is stored in ans. The function returns an integer value representing the total marks. The total marks are printed along with the average of the marks.

Overall, this code correctly computes the total marks and the average, and returns the total marks to the calling function. However, it would be better if the marklist function printed the total marks and average instead of main doing that. Also, the variable avg is not used after it is computed.

Source Code

#include<iostream>
using namespace std;
int marklist(int,int,int,int,int);
int main()
{
    int tam,eng,mat,sci,soc,tot,avg,ans;
    cout<<"\nEnter the Marks:";
    cin>>tam>>eng>>mat>>sci>>soc;
    ans=marklist(tam,eng,mat,sci,soc);
    cout<<"\nTotal:"<<ans;
    avg=ans/5;
    cout<<"\nAverage:"<<avg;
    return 0;
}
int marklist(int tam,int eng,int mat,int sci,int soc)
{
    int tot;
    tot=tam+eng+mat+sci+soc;
    return tot;
}
To download raw file Click Here

Output

Enter the Marks:90
80
70
60
50
Total:350
Average:70

Example Program : 4

The marklist function takes input for the five subject marks and calculates the total marks. This total marks value is returned to the main function where it is stored in the total variable. The average marks are then calculated and printed to the console.

The use of the marklist function helps to simplify the code and make it more modular. If the code needs to be modified in the future to include additional functionality, it can be easily added to the marklist function without changing the main function. Overall, this is a good example of how to use functions to improve the readability and maintainability of code.

Source Code

#include<iostream>
using namespace std;
int marklist();
int main()
{
    int avg,total;
    total=marklist();
    cout<<"\nTotal marks:"<<total;
    avg=total/5;
    cout<<"\nAverage:"<<avg;
    return 0;
}
int marklist()
{
    int tam,eng,mat,sci,soc,tot,avg,ans;
    cout<<"\nEnter the Marks:";
    cin>>tam>>eng>>mat>>sci>>soc;
    tot=tam+eng+mat+sci+soc;
    return tot;
}
To download raw file Click Here

Output

Enter the Marks:90
80
70
60
50
Total marks:350
Average:70

Program List


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples