Grade Calculator: Calculate and Display Grades Based on Input Scores

 

Introduction

In this tutorial, we will learn how to write a simple Grade Calculator program in C++ that calculates and displays grades based on input scores. The program will accept a numerical score as input and provide a corresponding grade according to predefined thresholds. By the end of this tutorial, you will be able to create a grade calculator that can be used to grade assignments, exams, or any other type of score.

Objective

The objective of this program is to:

  • Take a score input from the user.
  • Calculate the grade based on that score using specific thresholds.
  • Display the grade corresponding to the score.

C++ Code: Grade Calculator

#include 
using namespace std;

int main() {
    double score;
    char grade;

    // Ask user to input the score
    cout << "Enter your score (0-100): "; cin >> score;

    // Validate score input
    if (score < 0 || score > 100) {
        cout << "Invalid score! Please enter a score between 0 and 100."; return 1; } // Calculate grade based on the score if (score >= 90) {
        grade = 'A';
    }
    else if (score >= 80) {
        grade = 'B';
    }
    else if (score >= 70) {
        grade = 'C';
    }
    else if (score >= 60) {
        grade = 'D';
    }
    else {
        grade = 'F';
    }

    // Display the grade
    cout << "Your grade is: " << grade << endl;

    return 0;
}

Explanation of Program Structure

The program consists of the following steps:

  1. Input Score: The program prompts the user to enter a score between 0 and 100. The score is read from the keyboard using the cin function.
  2. Validation: The program checks if the score entered is within the valid range (0-100). If the score is invalid, an error message is displayed and the program terminates.
  3. Grade Calculation: Based on the score entered, the program uses conditional statements (if-else) to determine the grade. The grade is assigned according to predefined thresholds:
    • A for scores 90 and above
    • B for scores between 80 and 89
    • C for scores between 70 and 79
    • D for scores between 60 and 69
    • F for scores below 60
  4. Display Grade: The program displays the corresponding grade to the user using cout.

How to Run the Program

To run this program, follow these steps:

    1. Copy the C++ code into a text editor or an Integrated Development Environment (IDE) like Code::Blocks or Visual Studio.
    2. Save the file with a .cpp extension, for example, GradeCalculator.cpp.
    3. Compile the code using your IDE or by running the following command in your terminal or command prompt (ensure you have a C++ compiler like GCC installed):
g++ GradeCalculator.cpp -o GradeCalculator
    1. Run the compiled program by entering the following command:
./GradeCalculator
  1. Follow the on-screen instructions to input the score and receive the grade.
© 2025 Learn Programming. All rights reserved.

 

Leave a Reply

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