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++
#include <bits/stdc++.h>
using namespace std;
void percentile( int arr[], int n)
{
int i, j, count, percent;
for (i = 0; i < n; i++) {
count = 0;
for ( int j = 0; j < n; j++) {
if (arr[i] > arr[j]) {
count++;
}
}
percent = (count * 100) / (n - 1);
cout << "\nPercentile of Student "
<< i + 1 << " = " << percent;
}
}
int main()
{
int StudentMarks[] = { 12, 60, 80, 71, 30 };
int n = sizeof (StudentMarks) / sizeof (StudentMarks[0]);
percentile(StudentMarks, n);
return 0;
}
|
Java
class GFG
{
static void percentile( int arr[], int n)
{
int i, count, percent;
for (i = 0 ; i < n; i++)
{
count = 0 ;
for ( int j = 0 ; j < n; j++)
{
if (arr[i] > arr[j])
{
count++;
}
}
percent = (count * 100 ) / (n - 1 );
System.out.print( "\nPercentile of Student "
+ (i + 1 ) + " = " + percent);
}
}
public static void main(String[] args)
{
int [] StudentMarks = { 12 , 60 , 80 , 71 , 30 };
int n = StudentMarks.length;
percentile(StudentMarks, n);
}
}
|
Python
def percentile(arr, n):
i, j = 0 , 0
count, percent = 0 , 0
while i < n:
count = 0
j = 0
while j < n:
if (arr[i] > arr[j]):
count + = 1
j + = 1
percent = (count * 100 ) / / (n - 1 )
print ( "Percentile of Student " , i + 1 , " = " , percent)
i + = 1
StudentMarks = [ 12 , 60 , 80 , 71 , 30 ]
n = len (StudentMarks)
percentile(StudentMarks, n)
|
C#
using System;
class GFG
{
static void percentile( int []arr, int n)
{
int i, count, percent;
for (i = 0; i < n; i++)
{
count = 0;
for ( int j = 0; j < n; j++)
{
if (arr[i] > arr[j])
{
count++;
}
}
percent = (count * 100) / (n - 1);
Console.Write( "\nPercentile of Student "
+ (i + 1) + " = " + percent);
}
}
public static void Main()
{
int [] StudentMarks = {12, 60, 80, 71, 30};
int n = StudentMarks.Length;
percentile(StudentMarks, n);
}
}
|
Javascript
<script>
function percentile(arr, n)
{
var i, count, percent;
for (i = 0; i < n; i++)
{
count = 0;
for ( var j = 0; j < n; j++)
{
if (arr[i] > arr[j])
{
count++;
}
}
percent = (count * 100) / (n - 1);
document.write( "\nPercentile of Student "
+ (i + 1) + " = " + percent + "<br>" );
}
}
var StudentMarks = [ 12, 60, 80, 71, 30 ];
var n = StudentMarks.length;
percentile(StudentMarks, n);
</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)
Share your thoughts in the comments
Please Login to comment...