Open In App

R Program to Assign Grades Based on a Student’s Score Using If-Else

Last Updated : 27 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Giving pupils grades based on their performance is a frequent practise in educational environments. This program’s goal is to automatically grade students in accordance with their test results. The programme will assess a student’s performance and compute the corresponding grade using if-else statements in R. This automation not only speeds up the grading process but also guarantees fairness and consistency.

The If-Else statement is used when we need to perform a particular task based on a condition. The If block executes if the condition is true else the Else block is executed.

Grade Assignment Based on Percentage for Student’s Marks

Given an integer array marks, which comprises of marks scored by a student (out of 100) in different subjects, the task is to assign a grade to the student. The grade is found out by taking the percentage of the marks scored by the student. The percentage is calculated as:

(Total Marks Obtained by the student ✖ 100 ) / (Maximum marks of all the subjects)

The grade is assigned using the following rules

Percentage

Grade

90 And above

A

80 to 89

B

60 to 79

C

33 to 59

D

Below 33

E

Approach:

Input: marks = { 25, 65, 46, 98, 78, 65 }
Output: C
Explanation: Total marks obtained by the student = 25 + 64+ 46+ 98 + 78+ 65 = 376
Max_marks = 5 ✖ 100 = 500
Percentage = ( 376 ✖ 100) / 500
= 37600 / 500 = 73. 2 (So the grade will be C, according to the above table)

Input: marks = { 95, 88, 98, 93, 92, 96 }
Output: A
Explanation: Total marks obtained by the student = 95 + 88+ 98 +93 +92 +96 = 562
Max_marks = 6 ✖ 100 = 600
Percentage = ( 562 ✖ 100) / 600
= 56200 / 600 = 93. 6 (So the grade will be A, according to the above table)

Below is the approach to solve the problem:

  • Initialize a variable to sum all the marks scored by the student, total to 0.
  • Initialize a variable to store the grade of the student, grade to ‘F’.
  • First, we iterate through the marks array and find the total marks scored by the student.
  • Then, we apply the formula described above to calculate the percentage.
  • We then make a nested if else construct to assign proper grade to the student.

Below is the implementation of the above approach in R language:

R




# R program to assign
#grades to a student
# using nested if-else
 
#Store marks of all the
#subjects in an array
marks <- c(95, 88, 98, 93, 92, 96)
 
#Max marks will be
#100 * number of subjects
max_marks <- length(marks) * 100
 
# Initialize student's
#total marks to 0
 
total <- 0
 
# Initialize student's
# grade marks to F
 
grade<- 'F'
 
#find the sum of the array
 
 total <- sum(marks)
 
# Calculate the percentage.
percentage <- (total/ max_marks) *100
 
# Nested if else
if (percentage >= 90) {
  grade <- 'A'
} else {
  if (percentage >= 80 && percentage <= 89) {
    grade <- 'B'
  } else {
    if (percentage >= 60 && percentage <= 79) {
      grade <- 'C'
    } else {
      if (percentage >= 33 && percentage <= 59) {
        grade <- 'D'
      } else {
        grade <- 'F'
      }
    }
  }
}
 
print(grade)


Output:

[1] "A"

Time complexity: O(N), Where N is the number of elements in the array.
Auxiliary Space : O(1), constant space is used.

Here in the above code we have find out the sum of the array elements using sum() function. and then find the percentage of the marks obtained by the students by using the formula mentioned in the article, Based on their percentage, we have given Grades(A, B, C, D, E) to the students.

Grade Classification on student’s score using If-Else

Suppose There is a class- test of Programming Language, In which students got different marks Out of 50. If the students will get some Grade Based on the below table:

Marks Out of 50

Grade

above 40

Excellent

30-40

Very Good

20- 30

Good

below 20

Bad

Write a program to assign grades based on student’s score using If -Else Satatement in R:

Below is the implementation using If-Else Statement:

R




# Function to assign grades based on marks
assignGrade <- function(marks) {
  if (marks > 40) {
    return("Excellent")
  } else if (marks >= 30 && marks <= 40) {
    return("Very Good")
  } else if (marks >= 20 && marks < 30) {
    return("Good")
  } else {
    return("Bad")
  }
}
 
# Example usage
student1_marks <- 45
student2_marks <- 35
student3_marks <- 25
 
grade1 <- assignGrade(student1_marks)
grade2 <- assignGrade(student2_marks)
grade3 <- assignGrade(student3_marks)
 
cat("Student 1 got:", student1_marks, "marks. Grade:", grade1, "\n")
cat("Student 2 got:", student2_marks, "marks. Grade:", grade2, "\n")
cat("Student 3 got:", student3_marks, "marks. Grade:", grade3, "\n")


Output:

Student 1 got: 45 marks. Grade: Excellent 
Student 2 got: 35 marks. Grade: Very Good
Student 3 got: 25 marks. Grade: Good

In the above program, we have declared a function that will print the Grade of the Students according to their Marks using If-else Statement.

Conclusion

In conclusion, if-else programming expressions are a useful tool for automating the grading process based on student achievement. We guarantee fairness and uniformity in the grading process by analysing their results and implementing particular guidelines. These programmes streamline the grading process and save time for teachers, whether they use a predefined grading scale or percentage calculations to determine grades. This strategy improves the precision and objectivity of grade assignment in educational settings while also being effective.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads