Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors

# Program to calculate student exam grade

## Write a program that takes a student’s score as input and calculates the corresponding grade based on the following conditions:

• 90 or above: A
• 80 to 89: B
• 70 to 79: C
• 60 to 69: D
• Below 60: F

# Program in C

C
``````#include <stdio.h>

int main() {
int score;

printf("Enter the student's score: ");
scanf("%d", &score);

if (score >= 90) {
} else if (score >= 80 && score <= 89) {
} else if (score >= 70 && score <= 79) {
} else if (score >= 60 && score <= 69) {
} else {
}

return 0;
}``````

## Explanation:

• In this program, we declare an integer variable score to store the student’s score.
• We use printf to prompt the user to enter the student’s score, and scanf to read the input from the user and store it in the score variable.
• Next, we use a series of if and else if statements to determine the corresponding grade based on the score.
• The first if statement checks if the score is greater than or equal to 90. If this condition is true, it means the score is 90 or above, and we print “Grade: A” using printf.
• If the first if condition is false, we move to the else if statement.
• This statement checks if the score is between 80 and 89 (inclusive). If this condition is true, it means the score is in the range of 80 to 89, and we print “Grade: B” using printf.
• Similarly, we use else if statements to check the score against the remaining grade ranges: 70 to 79, 60 to 69, and below 60.
• If the score falls within any of these ranges, the corresponding grade is printed using printf.
• If none of the conditions in the if and else if statements are true, it means the score is below 60, and we print “Grade: F” using printf.
• Finally, we return 0 to indicate successful execution of the program.

## Output:

Output
``````Enter the student's score: 80

# Program in Java

Java
``````import java.util.Scanner;

public static void main(String[] args) {
int score;

Scanner scanner = new Scanner(System.in);
System.out.print("Enter the student's score: ");
score = scanner.nextInt();

if (score >= 90) {
} else if (score >= 80 && score <= 89) {
} else if (score >= 70 && score <= 79) {
} else if (score >= 60 && score <= 69) {
} else {
}

scanner.close();
}
}``````

## Explanation:

1. The program starts by importing the Scanner class from the java.util package, which allows us to read input from the user.
2. The GradeCalculator class is defined, which contains the main method where the program execution begins.
3. Inside the main method, an integer variable score is declared to store the student’s score.
4. A Scanner object named scanner is created to read input from the user.
5. The program prompts the user to enter the student’s score using the System.out.print() method.
6. The nextInt() method of the Scanner class is used to read an integer value entered by the user and assign it to the score variable.
7. The program then uses a series of if and else if statements to determine the grade based on the score:
• If the score is greater than or equal to 90, the program prints “Grade: A”.
• If the score is between 80 and 89 (inclusive), the program prints “Grade: B”.
• If the score is between 70 and 79 (inclusive), the program prints “Grade: C”.
• If the score is between 60 and 69 (inclusive), the program prints “Grade: D”.
• If none of the above conditions are true, the program prints “Grade: F”.
8. Finally, the scanner.close() method is called to release any resources associated with the Scanner object and close the input stream.
Java Output
``````Enter the student's score: 84