[Solved] PTM tomorrow in Cambridge School. Teachers want to show the grades

It’s PTM tomorrow in Cambridge School. Teachers want to show the grades from(A to E) depending on the percentage of the student. Help the teacher’s by developing the program for the same.To find the grade of a student, given the marks of N subjects. Given the marks of N subjects, we have to print the grade of a student based on the following grade slab.

If Percentage Marks > 90, Grade is A+ If 70 <= Percentage Marks <= 89, Grade is A If 60 <= Percentage Marks <= 69, Grade is B If 50 <= Percentage Marks <= 59, Grade is C If Percentage Marks <= 40, Grade is D

Input Format

Integer Value to enter number of subjects, count Enter marks of subjects depending upon the the total number of subjects

Constraints

Number of subjects should not exceed 7

Output Format

Character output to show grades

Sample Input 0

5
50 57 89 87 56
Sample Output 0

B
Sample Input 1

9
Sample Output 1

Invalid

Solution

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        
        Scanner sc = new Scanner(System.in);
        int count = sc.nextInt();

        if (count <= 0 || count > 7) {
            System.out.println("Invalid");
            return;
        }

        int[] marks = new int[count];
        int totalMarks = 0;
        for (int i = 0; i < count; i++) {
            marks[i] = sc.nextInt();
            totalMarks += marks[i];
        }

        double percentage = (double) totalMarks / (count * 100) * 100;

        char grade;
        if (percentage > 90) {
            grade = 'A';
        } else if (percentage >= 70 && percentage <= 89) {
            grade = 'A';
        } else if (percentage >= 60 && percentage <= 69) {
            grade = 'B';

            // sauravhathi
        } else if (percentage >= 50 && percentage <= 59) {
            grade = 'C';
        } else {
            grade = 'D';
        }

        System.out.println(grade);
    }
}

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *