Grading Students HackerRank Solution in C, C++, Java, Python

HackerLand University has the following grading policy:

  • Every student receives a grade in the inclusive range from 0 to 100.
  • Any grade less than 40 is a failing grade.

Sam is a professor at the university and likes to round each student’s grade according to these rules:

  • If the difference between the grade and the next multiple of 5 is less than 3, round grade up to the next multiple of 5.
  • If the value of grade is less than 38, no rounding occurs as the result will still be a failing grade.

Examples

  •  grade = 84 round to 85 (85 – 84 is less than 3)
  •  grade = 29 do not round (result is less than 40)
  •  grade do not round (60 – 57 is 3 or higher)

Given the initial value of  for each of Sam’s  students, write code to automate the rounding process.

Function Description

Complete the function gradingStudents in the editor below.

gradingStudents has the following parameter(s):

  • int grades[n]: the grades before rounding

Returns

  • int[n]: the grades after rounding as appropriate

Input Format

The first line contains a single integer,n, the number of students.

Each line i of the n subsequent lines contains a single integer, grade[i].

Constraints

  • 1<=n<=60
  • 0<=grades[i]<=100

Sample Input 0

4

73

67

38

33

 

Sample Output 0

75

67

40

33

 

Grading Students HackerRank Solution in C

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

int main(){
int n;
scanf("%d",&n);
for(int a0 = 0; a0 < n; a0++){
int grade;
scanf("%d",&grade);
// your code goes here
int x = (grade+4)/5;
x *= 5;
if(x>=40 && x-grade<3)grade=x;
printf("%d\n",grade);
}
return 0;
}

 

Grading Students HackerRank Solution in C++

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <unordered_map>

using namespace std;


int main(){
int n;
cin >> n;
for(int a0 = 0; a0 < n; a0++){
int grade;
cin >> grade;
if (grade >= 38) {
int rem = grade % 5;
if (rem >= 3) grade += 5 - rem;
}
cout << grade << endl;
}
return 0;
}

 

Grading Students HackerRank Solution in Java

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
for(int a0 = 0; a0 < n; a0++){
int grade = in.nextInt();
// your code goes here
if(grade < 38){
System.out.println(grade);
}
else{
int q = grade/5;
int rem = grade%5;
if(rem >= 3){
System.out.println((q+1)*5);
}
else{
System.out.println(grade);
}
}
}
}
}

 

Grading Students HackerRank Solution in Python

#!/bin/python

import sys


n = int(raw_input().strip())
for a0 in xrange(n):
grade = int(raw_input().strip())
# your code goes here
if grade < 38:
print grade
continue
if grade % 5 >= 3:
grade = ((grade / 5) + 1) * 5
print grade

 

Grading Students HackerRank Solution in C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution {

static void Main(String[] args) {
int n = Convert.ToInt32(Console.ReadLine());
for(int a0 = 0; a0 < n; a0++){
int grade = Convert.ToInt32(Console.ReadLine());
int final = 0;
int next = ((grade / 5) + 1) * 5;
if (next - grade >= 3 || grade < 38)
final = grade;
else
final = next;
Console.WriteLine(final);

}
}
}

 

Attempt – Grading Students HackerRank Challenge

Link – https://www.hackerrank.com/challenges/grading/

Leave a Comment