Skip to content

Instantly share code, notes, and snippets.

@xuhaibahmad
Created September 14, 2019 04:46
Show Gist options
  • Save xuhaibahmad/0e4e36cfbdd19b39789ff4c1d88f4198 to your computer and use it in GitHub Desktop.
Save xuhaibahmad/0e4e36cfbdd19b39789ff4c1d88f4198 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