Program to calculate Percentile of a student based on rank
Last Updated :
19 Apr, 2023
Given the rank of a student and the total number of students appearing in an examination, the task is to find the percentile of the student.
The percentile of a student is the % of the number of students having marks less than him/her.
Examples:
Input: Rank: 805, Total Number of Students Appeared: 97481
Output: 99.17
Explanation:
((97481 – 805) / 97481) * 100 = 99.17
Input: Rank: 65, Total Number of Students Appeared: 100
Output: 35
Explanation:
((100 – 65) / 100) * 100 = 35
Approach
The formula to calculate the percentile when the rank of the student and the total number of students appeared is given is:
((Total Students – Rank) / Total Students) * 100
Below is the implementation of the above formula:
C++
C++
#include <bits/stdc++.h>
using namespace std;
float getPercentile( int rank, int students)
{
float result = float (students - rank)
/ students * 100;
return result;
}
int main()
{
int your_rank = 805;
int total_students = 97481;
cout << getPercentile(
your_rank, total_students);
}
|
Java
import java.util.*;
class GFG{
static float getPercentile( int rank, int students)
{
float result = ( float )(students - rank)
/ students * 100 ;
return result;
}
public static void main(String[] args)
{
int your_rank = 805 ;
int total_students = 97481 ;
System.out.print(getPercentile(
your_rank, total_students));
}
}
|
Python3
def getPercentile(rank, students) :
result = (students - rank) / students * 100 ;
return result;
if __name__ = = "__main__" :
your_rank = 805 ;
total_students = 97481 ;
print (getPercentile(your_rank, total_students));
|
C#
using System;
class GFG{
static float getPercentile( int rank, int students)
{
float result = ( float )(students - rank)
/ students * 100;
return result;
}
public static void Main(String[] args)
{
int your_rank = 805;
int total_students = 97481;
Console.Write(getPercentile(
your_rank, total_students));
}
}
|
Javascript
<script>
function getPercentile(rank , students)
{
var result = (students - rank) / students * 100;
return result;
}
var your_rank = 805;
var total_students = 97481;
document.write(getPercentile(your_rank, total_students).toFixed(4));
</script>
|
Performance Analysis:
- Time Complexity: In the above approach, we are able to calculate percentile using a formula in constant time, so the time complexity is O(1).
- Auxiliary Space Complexity: In the above approach, we are not using any extra space apart from a few constant size variables, so Auxiliary space complexity is O(1).
Share your thoughts in the comments
Please Login to comment...