Java
Java

 

Introduction

Grading is an important aspect of education, and this program helps you calculate and display the grade of a student based on their score. It takes the input score, checks it against predefined grade ranges, and returns the corresponding grade.
This is a simple implementation in Java that can be easily expanded or modified according to specific grading criteria.

Objective

The objective of this program is to take a score input from the user and display the corresponding grade based on predefined score ranges. The grades are typically:

  • 90-100: A
  • 80-89: B
  • 70-79: C
  • 60-69: D
  • Below 60: F

Java Code for Grade Calculator


import java.util.Scanner;

public class GradeCalculator {

    public static void main(String[] args) {
        // Create a scanner object to take user input
        Scanner scanner = new Scanner(System.in);

        // Prompt user to enter the score
        System.out.print("Enter your score: ");
        int score = scanner.nextInt();

        // Variable to store the grade
        char grade;

        // Calculate grade based on score
        if (score >= 90 && score <= 100) { grade = 'A'; } else if (score >= 80 && score < 90) { grade = 'B'; } else if (score >= 70 && score < 80) { grade = 'C'; } else if (score >= 60 && score < 70) {
            grade = 'D';
        } else {
            grade = 'F';
        }

        // Display the grade
        System.out.println("Your grade is: " + grade);

        // Close the scanner
        scanner.close();
    }
}
            

Explanation of the Program Structure

The program consists of the following components:

  1. Import Scanner: The Scanner class is used to get input from the user.
  2. Input Section: The program prompts the user to input a score. The input score is read using scanner.nextInt().
  3. Conditional Statements: The if-else if structure evaluates the score and assigns the correct grade.
  4. Grade Output: After calculating the grade, the program outputs the grade using System.out.println().

How to Run the Program

  1. Copy the provided Java code into a text file and save it as GradeCalculator.java.
  2. Open a terminal or command prompt.
  3. Navigate to the directory where you saved GradeCalculator.java.
  4. Compile the Java program using the command:
    javac GradeCalculator.java
  5. Run the program by executing the following command:
    java GradeCalculator
  6. The program will prompt you to enter a score, and it will display the corresponding grade.
© 2025 Learn Programming. All Rights Reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

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

error

Enjoy this blog? Please spread the word :)