Find the Letter Grade of a Student in C / C++ / Java / Python / C#

Grade of a Student

Programs to find the letter grade of a student are shown here. The letter grade is calculated according to the score obtained by the students. The following table depicts the grade table and the ranges of the score.


Classification Score Letter Grade
Outstanding 90 to 100 O
Excellent 80 to below 90 E
Very good 70 to below 80 A
Good 60 to below 70 B
Fair 50 to below 60 C
Below average 40 to below 50 D
Failed Below 40 F

For example, if the score is 65, then according to the table, the grade is B, and if the score is 93, then it is O.






1. Algorithm to Find the Letter Grade of a Student


1. Take the score as input.

2. If the score is greater than 100 or less than 0, display the score as invalid.

3. If the score is between 90 and 100, display letter grade O as the output.

4. If the score is between 80 and below 90, display letter grade E as the output.

5. If the score is between 70 and below 80, display letter grade A as the output.

6. If the score is between 60 and below 70, display letter grade B as the output.

7. If the score is between 50 and below 60, display letter grade C as the output.

8. If the score is between 40 and below 50, display letter grade D as the output.

9. If the score is below 40, display letter grade F as the output.




2. Pseudocode to Find the Letter Grade of a Student


Input : Student score $s$

Output : Letter grade of the score $s$

1. Procedure letterGrade($s$):

2. If $s > 100$ or $s < 0$:

3. Return "Invalid score!"

4. Else If $s >= 90$:

5. Return "O"

6. Else If $s >= 80$:

7. Return "E"

8. Else If $s >= 70$:

9. Return "A"

10. Else If $s >= 60$:

11. Return "B"

12. Else If $s >= 50$:

13. Return "C"

14. Else If $s >= 40$:

15. Return "D"

16. Else:

17. Return "F"

18. End Procedure





3. Time complexity to Find the Letter Grade of a Student


Time Complexity: O(1)





4. Program to Find the Letter Grade of a Student

In this section, the C, C++, Java, Python and C# programs to find the letter grade w.r.t score are given. A valid range of score is 0 to 100.




4.1. C Program to Find the Letter Grade of a Student

Code has been copied
/**********************************************
             alphabetacoder.com
C program to find the letter grade of a student
***********************************************/
#include <stdio.h>

int main() {
    // declare variables
    double score;

    // take input
    printf("Enter the score: ");
    scanf("%lf", & score);

    // find the grade
    if (score > 100 || score < 0)
        printf("Invalid score!");
    else if (score >= 90)
        printf("Letter grade: O");
    else if (score >= 80)
        printf("Letter grade: E");
    else if (score >= 70)
        printf("Letter grade: A");
    else if (score >= 60)
        printf("Letter grade: B");
    else if (score >= 50)
        printf("Letter grade: C");
    else if (score >= 40)
        printf("Letter grade: D");
    else
        printf("Letter grade: F");

    return 0;
}

Output


Case 1:

Enter the score: 92

Letter grade: O


Case 2:

Enter the score: 65.5

Letter grade: B


Case 3:

Enter the score: 73

Letter grade: A




4.2. C++ Program to Find the Letter Grade of a Student

Code has been copied
/************************************************
               alphabetacoder.com
C++ program to find the letter grade of a student
*************************************************/

#include <iostream>

using namespace std;

int main() {
    // declare variables
    double score;

    // take input
    cout << "Enter the score: ";
    cin >> score;

    // find the grade
    if (score > 100 || score < 0)
        cout << "Invalid score!";
    else if (score >= 90)
        cout << "Letter grade: O";
    else if (score >= 80)
        cout << "Letter grade: E";
    else if (score >= 70)
        cout << "Letter grade: A";
    else if (score >= 60)
        cout << "Letter grade: B";
    else if (score >= 50)
        cout << "Letter grade: C";
    else if (score >= 40)
        cout << "Letter grade: D";
    else
        cout << "Letter grade: F";

    return 0;
}

Output


Case 1:

Enter the score: 38

Letter grade: F


Case 2:

Enter the score: 56.75

Letter grade: C


Case 3:

Enter the score: 49

Letter grade: D




4.3. Java Program to Find the Letter Grade of a Student

Code has been copied
/*************************************************			    
               alphabetacoder.com
Java program to find the letter grade of a student
**************************************************/

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // declare variables
        double score;

        // declare instance of Scanner class
        Scanner sc = new Scanner(System.in);

        // take input
        System.out.print("Enter the score: ");
        score = sc.nextDouble();

        // find the grade
        if (score > 100 || score < 0)
            System.out.print("Invalid score!");
        else if (score >= 90)
            System.out.print("Letter grade: O");
        else if (score >= 80)
            System.out.print("Letter grade: E");
        else if (score >= 70)
            System.out.print("Letter grade: A");
        else if (score >= 60)
            System.out.print("Letter grade: B");
        else if (score >= 50)
            System.out.print("Letter grade: C");
        else if (score >= 40)
            System.out.print("Letter grade: D");
        else
            System.out.print("Letter grade: F");
    }
}

Output


Case 1:

Enter the score: 38

Letter grade: F


Case 2:

Enter the score: 56.75

Letter grade: C


Case 3:

Enter the score: 49

Letter grade: D




4.4. Python Program to Find the Letter Grade of a Student

Code has been copied
# ****************************************************
#               alphabetacoder.com
# Python program to find the letter grade of a student
# ****************************************************

# take input
score = float(input("Enter the score: "))

# find the grade
if score > 100 or score < 0:
    print("Invalid score!")
elif score >= 90:
    print("Letter grade: O")
elif score >= 80:
    print("Letter grade: E")
elif score >= 70:
    print("Letter grade: A")
elif score >= 60:
    print("Letter grade: B")
elif score >= 50:
    print("Letter grade: C")
elif score >= 40:
    print("Letter grade: D")
else:
    print("Letter grade: F")

Output


Case 1:

Enter the score: 105

Invalid score!


Case 2:

Enter the score: 47.5

Letter grade: D


Case 3:

Enter the score: 62

Letter grade: B




4.5. C# Program to Find the Letter Grade of a Student

Code has been copied
/*************************************************
	      alphabetacoder.com
C# program to find the letter grade of a student
**************************************************/

using System;

namespace FindGrade {
    class Program {
        static void Main(string[] args) {
            // declare variables
            double score;

            // take input
            Console.Write("Enter the score: ");
            score = Convert.ToDouble(Console.ReadLine());

            // find the grade
    	    if(score > 100 || score < 0)
    	        Console.WriteLine("Invalid score!");
    	    else if (score >= 90)
    	        Console.WriteLine("Letter grade: O");
    	    else if (score >= 80)
    	        Console.WriteLine("Letter grade: E");
    	    else if (score >= 70)
    		Console.WriteLine("Letter grade: A");
    	    else if (score >= 60)
    	        Console.WriteLine("Letter grade: B");
    	    else if (score >= 50)
    		Console.WriteLine("Letter grade: C");
    	    else if (score >= 40)
    	        Console.WriteLine("Letter grade: D");
    	    else
    		Console.WriteLine("Letter grade: F");

            // wait for user to press any key
            Console.ReadKey();
        }
    }
}

Output


Case 1:

Enter the score: -5

Invalid score!


Case 2:

Enter the score: 39

Letter grade: F


Case 3:

Enter the score: 79

Letter grade: A