codescracker
c

C Program to Calculate Average & Percentage Marks



« Previous Program Next Program »

In this article, you will learn and get code about how to create a program that finds average and percentage marks. The last program given here is the complete version of finding average and percentage marks of a student. In that program, number of subject and maximum marks is also decided by user.

Find Average Mark Program in C

The question is, write a program in C, that asks from user to enter marks obtained in 5 subjects and prints average mark as output. So the answer to this question is given below:

// Find Average Marks of Students in C
// ------codescracker.com-------

#include<stdio.h>
#include<conio.h>
int main()
{
    int i;
    float mark, sum=0, avg;
    printf("Enter Marks obtained in 5 Subjects: ");
    for(i=0; i<5; i++)
    {
        scanf("%f", &mark);
        sum = sum+mark;
    }
    avg = sum/5;
    printf("\nAverage Mark = %0.2f", avg);
    getch();
    return 0;
}

This program was compiled and executed in Code::Blocks IDE. This is the first snapshot of sample run:

c program average percentage marks

Now supply marks obtained in 5 subjects as input say 76, 87, 98, 70, 82 and press ENTER key to see the average mark of student as shown in the second snapshot of sample run given below:

average mark program c

Steps used in previous Program

Here are some of the main steps used in above program:

To learn about loop (used in previous program) in detail, refer to for loop in C tutorial. Let's move on to next program.

Find Percentage Mark Program in C

The question is, write a program in C, that asks from user to enter marks obtained in 5 subjects (out of 100) and find the percentage marks of student. The answer to this question is given below:

To calculate percentage marks of a student in C programming, you have to ask from user to enter marks obtained in some number of subjects (5 subjects here, i.e Physics, Chemistry, Maths, CS, and English). Place summation of 5 marks in a variable say sum and place sum/5 in a variable say avg. Simply print avg as the result on output.

// Find Percentage Marks of Students in C
// ------codescracker.com-------

#include<stdio.h>
#include<conio.h>
int main()
{
    int i;
    float mark, sum=0, perc;
    printf("Enter Marks obtained in 5 Subjects (Out of 100): ");
    for(i=0; i<5; i++)
    {
        scanf("%f", &mark);
        sum = sum+mark;
    }
    perc = (sum/500)*100;
    printf("\nPercentage Mark = %0.2f%c", perc, 37);
    getch();
    return 0;
}

This program will produce the output as shown in the snapshot given below:

percentage mark program c

Most of the code written in this program is almost same as of previous program except, here we have used %c format specifier with 37 as its value. Because, %c format specifier is used to print character, and the ASCII value of % character is 37. So to print % sign on output screen after the percentage value, we have used its ASCII value as shown in the above output.

Find Average & Percentage Mark in C

The question is, write a program in C that asks from user to enter marks obtained and find average and percentage mark value. Here subject count and maximum marks must be entered by user. The answer to this question is given below.

This is the complete version of calculating average and percentage marks program in C. Here the number of subject and maximum mark is decided by user. Let's have a look at it:

// Find Average and Percentage Mark in C
// ------codescracker.com-------

#include<stdio.h>
#include<conio.h>
int main()
{
    int i, n, max;
    float mark, sum=0, avg, perc;
    printf("How many Subject runs in your Institute ? ");
    scanf("%d", &n);
    printf("\nEnter Maximum Mark of Subject: ");
    scanf("%d", &max);
    printf("\nEnter Marks obtained in %d Subjects (Out of %d): ", n, max);
    for(i=0; i<n; i++)
    {
        scanf("%f", &mark);
        sum = sum+mark;
    }
    avg = sum/n;
    perc = (sum/(n*max))*100;
    printf("\nAverage Mark = %0.2f", avg);
    printf("\nPercentage Mark = %0.2f%c", perc, 37);
    getch();
    return 0;
}

The snapshot given below shows the initial output of this program:

average percentage mark program c

And the snapshot given below shows the final output of this program after supplying total number of subject runs in institute, maximum marks, and marks obtained in all subject.

c average percentage mark

Let's understand the above program (with its output) by the example given below:

Same Program in Other Languages

C Online Test


« Previous Program Next Program »



© Copyright 2021. All Rights Reserved.

CodesCracker