C Program To Calculate Average and Percentage of Marks Obtained


Write a function that receives marks received by a student in 3 subjects and returns the average and percentage of these marks. Call this function from main() and print the results in main().

Analyze The Above Problem Statement

1. We need to write a user defined function.
2. Our function will be passed with 3 floating point numbers.
3. Since the problem statement is asking us to return more than 1 value, we need to make use of pointer concept.
4. We will not be using arrays in this program since the number of inputs is fixed to 3. We can take 3 variables to store the values entered/input by the user. i.e., marks obtained by the student in 3 subjects.

Related Read:
Basics of Pointers In C Programming Language

Very Important Note:

Any address preceded by a * (Indirection operator) will fetch the value present at that address.

Video Tutorial: C Program To Calculate Average and Percentage of Marks Obtained using Pointer


[youtube https://www.youtube.com/watch?v=P8iLhG0ManM]

YouTube Link: https://www.youtube.com/watch?v=P8iLhG0ManM [Watch the Video In Full Screen.]


Source Code: C Program To Calculate Average and Percentage of Marks Obtained

#include<stdio.h>

void percentage(float, float, float, int*,
                float*, float*, float*);

int main()
{
    float a, b, c, total = 0, avg = 0, percent = 0;
    int max;

    printf("Enter marks obtained in 3 subjects\n");
    scanf("%f%f%f", &a, &b, &c);

    printf("What's the total marks(of 3 subjects combined) 
            to which the exam was conducted\n");
    scanf("%d", &max);

    percentage(a, b, c, &max, &total, &avg, &percent);

    printf("\nTotal Marks Obtained = %0.2f\n", total);
    printf("Average = %0.2f\n", avg);
    printf("Percentage = %0.2f\n", percent);

    return 0;
}

void percentage(float x, float y, float z, int *max,
                float *tot, float *avg, float *per)
{
    *tot = x + y + z;
    *avg = *tot / 3.0;

    *per = *tot * 100.0 / (float)*max;
}

Output 1:
Enter marks obtained in 3 subjects
42
41
32
What’s the total marks(of 3 subjects combined) to which the exam was conducted
150

Total Marks Obtained = 115.00
Average = 38.33
Percentage = 76.67

Output 2:
Enter marks obtained in 3 subjects
123
145
140
What’s the total marks(of 3 subjects combined) to which the exam was conducted
450

Total Marks Obtained = 408.00
Average = 136.00
Percentage = 90.67

Output 3:
Enter marks obtained in 3 subjects
92
85
96
What’s the total marks(of 3 subjects combined) to which the exam was conducted
300

Total Marks Obtained = 273.00
Average = 91.00
Percentage = 91.00

Logic To Calculate Average and Percentage of Marks Obtained

We ask the user to input marks obtained by the student for 3 subjects. And then we also ask the user to input the maximum marks(for 3 subjects combined) for which the exam was conducted, and we store it in variable max.

We pass the values of a, b and c, and the address of max, total, avg and precent to percentage() function.

Inside percentage() Function
Inside percentage() function we calculate the Total marks obtained, average marks and percentage for the 3 subjects of the student and store it back as values at the address passed.

We make use of following formulas:
Total = a + b + c;
Average = Total / 3.0;

we are dividing Total by 3.0 because we have 3 subjects. It represents the number of subjects for which we’re calculating the Average.

percentage = Total x 100.0 / Max;

Here Max is the maximum marks(for all the subjects combined) for which the exam was conducted.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Leave a Reply

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