Skip to content

Instantly share code, notes, and snippets.

@xuhaibahmad
Created March 30, 2020 06:56
Show Gist options
  • Save xuhaibahmad/e9e1a98d78d15317c5270f857924de3e to your computer and use it in GitHub Desktop.
Save xuhaibahmad/e9e1a98d78d15317c5270f857924de3e to your computer and use it in GitHub Desktop.
class GradeCalculator {
var totalMarks = 0
fun getGrade(obtainedMarks: Int, totalMarks: Int): String {
val percentage = getPercentage(obtainedMarks, totalMarks)
return when {
percentage >= 90 -> "A"
percentage in 80..89 -> "B"
percentage in 70..79 -> "C"
percentage in 60..69 -> "D"
else -> "F"
}
}
private fun getPercentage(obtainedMarks: Int, totalMarks: Int): Int {
return (obtainedMarks / totalMarks.toFloat() * 100).roundToInt()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment