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

C Program To Calculate Percentage Difference Between 2 Numbers

Given two numbers, write a C program to calculate percentage difference between those two numbers.

Formula To Calculate Percentage Difference Between Two Numbers

If user entered numbers are stored in variables a and b. Then the formula to calculate the percentage difference between a and b is:

result = ( |a-b| / ((a+b) / 2.0) ) * 100;

OR

result = ( abs(a-b) / ((a+b) / 2.0) ) * 100;

Note: abs() is a builtin method present in stdlib.h header file, which returns the absolute value.

Related Read:
Basic Arithmetic Operations In C

Check this video tutorial to know more about absolute value:
C Program To Find Absolute Value of a Number

Logic To Calculate Percentage Difference Between 2 Numbers

The percentage difference between two numbers is calculated by dividing the absolute value of the difference between two numbers by the average of those two numbers. And then multiplying the result by 100.

Expected Output for the Input

User Input:
Enter 2 numbers
100
200

Output:
Percentage difference is 66.666664

Video Tutorial: C Program To Calculate Percentage Difference Between 2 Numbers


[youtube https://www.youtube.com/watch?v=L7Vc7-1z9os]

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

Source Code: C Program To Calculate Percentage Difference Between 2 Numbers

#include<stdio.h>
#include<stdlib.h>

int main()
{
    float a, b, result;

    printf("Enter 2 numbers\n");
    scanf("%f%f", &a, &b);

    result = ( abs(a-b) / ( (a+b)/2.0 ) ) * 100;

    printf("Percentage difference is %f\n", result);

    return 0;
}

Output 1:
Enter 2 numbers
50
60
Percentage difference is 18.181818

Output 2:
Enter 2 numbers
60
50
Percentage difference is 18.181818

Output 3:
Enter 2 numbers
75
75
Percentage difference is 0.000000

Output 4:
Enter 2 numbers
75
100
Percentage difference is 28.571428

Output 5:
Enter 2 numbers
150
200
Percentage difference is 28.571428

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

else if statement in C

In this program we’ll show you the usage of else-if clause in C programming language. Here we are illustrating the concept by taking score/marks of 5 subject from the user. Then we calculate the percentage of it and display grade to the user.

Related Read:
Nested if else Statement In C

Simple Logic: Student Grade

First in if‘s condition we check if the user entered percentage is above 100%. If true – in that case we let the user know that he entered wrong marks and he / she needs to re-enter the marks. Next in first else if we check if the percentage is greater than or equal to 60. Here we need not check if the percentage is less than 100, because in if clause itself we’ve checked if percentage is greater than 100, and we are checking the else if condition only because the condition in if is false. So percentage will be less than 100. Same logic goes to consecutive Grades.

Student Grade Calculation using else if clause: C Program


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

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


Note: When the condition is true, it executes its corresponding block of code and then skips checking remaining else if or else conditions.

Note: Its optional to have else in if or else if clause.

Note: We can have any number of else if statements after if.

Student Grade Calculation using else-if clause

 
#include < stdio.h >

int main()
{
    int s1, s2, s3, s4, s5, per;

    printf("Enter marks of 5 subjects\n");
    scanf("%d %d %d %d %d", &s1, &s2, &s3, &s4, &s5);

    per = (s1 + s2 + s3 + s4 + s5) / 5.0;

    if(per > 100)
        printf("You've entered wrong marks, kindly re-enter\n");
    else if(per >= 60)
        printf("Grade A\n");
    else if(per >= 50)
        printf("Grade B\n");
    else if(per >= 40)
        printf("Grade C\n");
    else
        printf("Failed!\n");

    return 0;
}

Output 1:
Enter marks of 5 subject
59
60
62
61
63
Grade A

Output 2:
Enter marks of 5 subject
59
55
56
53
58
Grade B

Output 3:
Enter marks of 5 subject
49
45
46
49
48
Grade C

Output 4:
Enter marks of 5 subject
39
45
25
36
35
Failed!

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

Nested if else Statement In C

In this program we’ll show you nesting of if else statements. Here we are illustrating the concept by taking score/marks of 5 subject from the user. Then we calculate the percentage of it and display grade to the user.

Simple Logic

When the condition in if statement is false then only else block gets executed. In our program, we check, if percentage is greater than or equal to 60. If it is false, then only control shifts to else block. So inside else(nested if else) we need not once again check if the percentage is less than 60.

Note: We are not using else if and logical operator in this C program purposefully. We want to show nesting of if else in this program.

 
#include < stdio.h >

int main()
{
    float s1, s2, s3, s4, s5, per;

    printf("Enter marks of 5 subject\n");
    scanf("%f %f %f %f %f", &s1, &s2, &s3, &s4, &s5);

    per = (s1 + s2 + s3 + s4 + s5) / 5.0;

    if(per >= 60)
    {
        printf("Grade A\n");
    }
    else
    {
        if(per >= 50)
        {
            printf("Grade B\n");
        }
        else
        {
            if(per >= 40)
            {
                printf("Grade C\n");
            }
            else
            {
                printf("Failed!\n");
            }
        }
    }

    return 0;
}

Output 1:
Enter marks of 5 subject
59
60
62
61
63
Grade A

Output 2:
Enter marks of 5 subject
59
55
56
53
58
Grade B

Output 3:
Enter marks of 5 subject
49
45
46
49
48
Grade C

Output 4:
Enter marks of 5 subject
39
45
25
36
35
Failed!

Student Grade Calculation using Nested if else: C Program


[youtube https://www.youtube.com/watch?v=I-EHRu-Nq8o]

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


Note: In next video we’ll show you how to perform the same operation as shown in above C program, using else-if and logical operators.

Also check the program C Program To Check Leap Year for a perfect nested if else example.

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