Program to calculate Percentile of Students

Last Updated : 19 Jul, 2022

Given an array containing marks of students, the task is to calculate the percentile of the students. The percentile is calculated according to the following rule: 
 

The percentile of a student is the % of the number of students having marks less than him/her.


Examples: 
 

Input: arr[] = { 12, 60, 80, 71, 30 }
Output: { 0, 50, 100, 75, 25 }
Explanation: 
Percentile of Student 1 = 0/4*100 = 0 (out of other 4 students no one has marks less than this student) 
Percentile of Student 2 = 2/4*100 = 50 (out of other 4 students, 2 have marks less than this student) 
Percentile of Student 3 = 4/4*100 = 100 (out of other 4 students, all 4 have marks less than this student) 
Percentile of Student 4 = 3/4*100 = 75 (out of other 4 students, 3 have marks less than this student) 
Percentile of Student 5 = 1/4*100 = 25 (out of other 4 students only 1 has marks less than this student) 
 


 


Approach: 
 

  • So basically, the percentile is a number where a certain percentage of scores fall below that number. 
     
  • For example: If in an examination a student's percentile is 75 then it means that the student has scored more than 75% of students who took the test. 
     
  • Now, in order to calculate percentile we have the following formula:
    PERCENTILE = (NUMBER OF STUDENTS WHO SCORED BELOW OR EQUAL TO THE DESIRED STUDENT/ TOTAL NUMBER OF STUDENTS - 1) * 100 
     


Below is the implementation of the above approach:
 

C++
// C++ program to calculate Percentile of Students

#include <bits/stdc++.h>
using namespace std;

// Function to calculate the percentile
void percentile(int arr[], int n)
{
    int i, j, count, percent;

    // Start of the loop that calculates percentile
    for (i = 0; i < n; i++) {

        count = 0;
        for (int j = 0; j < n; j++) {

            // Comparing the marks of student i
            // with all other students
            if (arr[i] > arr[j]) {
                count++;
            }
        }
        percent = (count * 100) / (n - 1);

        cout << "\nPercentile of Student "
             << i + 1 << " = " << percent;
    }
}

// Driver Code
int main()
{
    int StudentMarks[] = { 12, 60, 80, 71, 30 };
    int n = sizeof(StudentMarks) / sizeof(StudentMarks[0]);
    percentile(StudentMarks, n);

    return 0;
}
Java
// Java program to calculate Percentile of Students
class GFG
{

    // Function to calculate the percentile
    static void percentile(int arr[], int n)
    {
        int i, count, percent;

        // Start of the loop that calculates percentile
        for (i = 0; i < n; i++)
        {

            count = 0;
            for (int j = 0; j < n; j++) 
            {

                // Comparing the marks of student i
                // with all other students
                if (arr[i] > arr[j])
                {
                    count++;
                }
            }
            percent = (count * 100) / (n - 1);

            System.out.print("\nPercentile of Student "
            + (i + 1) + " = " + percent);
        }
    }

    // Driver Code
    public static void main(String[] args)
    {
        int[] StudentMarks = { 12, 60, 80, 71, 30 };
        int n = StudentMarks.length;
        percentile(StudentMarks, n);
    }
}

// This code is contributed by Rajput-Ji
Python
# Python3 program to calculate Percentile of Students

# Function to calculate the percentile
def percentile(arr, n):
    i, j = 0, 0
    count, percent = 0, 0

    # Start of the loop that calculates percentile
    while i < n:

        count = 0
        j = 0
        while j < n:

            # Comparing the marks of student i
            # with all other students
            if (arr[i] > arr[j]):
                count += 1
            j += 1
        percent = (count * 100) // (n - 1)

        print("Percentile of Student ", i + 1," = ", percent)
        i += 1

# Driver Code

StudentMarks = [12, 60, 80, 71, 30]
n = len(StudentMarks)
percentile(StudentMarks, n)

# This code is contributed by mohit kumar 29
C#
// C# program to calculate Percentile of Students
using System;

class GFG
{

    // Function to calculate the percentile
    static void percentile(int []arr, int n)
    {
        int i, count, percent;

        // Start of the loop that calculates percentile
        for (i = 0; i < n; i++)
        {
            count = 0;
            for (int j = 0; j < n; j++) 
            {

                // Comparing the marks of student i
                // with all other students
                if (arr[i] > arr[j])
                {
                    count++;
                }
            }
            percent = (count * 100) / (n - 1);

            Console.Write("\nPercentile of Student "
            + (i + 1) + " = " + percent);
        }
    }

    // Driver Code
    public static void Main()
    {
        int[] StudentMarks = {12, 60, 80, 71, 30};
        int n = StudentMarks.Length;
        percentile(StudentMarks, n);
    }
}

// This code is contributed by AnkitRai01
JavaScript
<script>

// Javascript program to calculate 
// Percentile of Students

// Function to calculate the percentile
function percentile(arr, n)
{
    var i, count, percent;

    // Start of the loop that calculates percentile
    for (i = 0; i < n; i++)
    {

        count = 0;
        for (var j = 0; j < n; j++) 
        {

            // Comparing the marks of student i
            // with all other students
            if (arr[i] > arr[j])
            {
                count++;
            }
        }
        percent = (count * 100) / (n - 1);

        document.write("\nPercentile of Student "
        + (i + 1) + " = " + percent + "<br>");
    }
}
    
// Driver Code
var StudentMarks = [ 12, 60, 80, 71, 30 ];
var n = StudentMarks.length;

percentile(StudentMarks, n);

// This code is contributed by Khushboogoyal499

</script>                    

Output: 
Percentile of Student 1 = 0
Percentile of Student 2 = 50
Percentile of Student 3 = 100
Percentile of Student 4 = 75
Percentile of Student 5 = 25

 

Time Complexity: O(n2)

Auxiliary Space: O(1)

Comment