36.7k views
2 votes
Write a program that has an input as a test score, and figures out a letter grade for that score, such as "A", "B", "C", etc. according to the scale: 90 or more - A 80 - 90 (excluding 90) - B 70 - 80 (excluding 80) - C 60 - 70 (excluding 70) - D else - F The program should print both a test score, and a corresponding letter grade. Test your program with different scores. Use a compound IF-ELSE IF statement to find a letter grade.

User Wuffwuff
by
6.6k points

1 Answer

5 votes

Answer:

The program in csharp for the given scenario is shown.

using System;

class ScoreGrade {

static void Main() {

//variables to store score and corresponding grade

double score;

char grade;

//user input taken for score

Console.Write("Enter the score: ");

score = Convert.ToInt32(Console.ReadLine());

//grade decided based on score

if(score>=90)

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';

//score and grade displayed

Console.WriteLine("Score: "+score+ " Grade: "+grade);

}

}

OUTPUT1

Enter the score: 76

Score: 76 Grade: C

OUTPUT2

Enter the score: 56

Score: 56 Grade: F

Step-by-step explanation:

1. The variables to hold the score and grade are declared as double and char respectively.

int score;

char grade;

2. The user is prompted to enter the score. The user input is not validated and the input is stored in the variable, score.

3. Using if-else-if statements, the grade is decided based on the value of the score.

if(score>=90)

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';

4. The score and the corresponding grade are displayed.

5. The program can be tested for different values of score.

6. The output for two different scores and two grades is included.

7. The program is saved using ScoreGrade.cs. The .cs extension indicates a csharp program.

8. The whole code is written inside a class since csharp is a purely object-oriented language.

9. In csharp, user input is taken using Console.ReadLine() which reads a string.

10. This string is converted into an integer using Convert.ToInt32() method.

score = Convert.ToInt32(Console.ReadLine());

11. The output is displayed using Console.WriteLine() or Console.Write() methods; the first method inserts a line after displaying the message which is not done in the second method.

12. Since the variables are declared inside Main(), they are not declared static.

13. If the variables are declared outside Main() and at the class level, it is mandatory to declare them with keyword, static.

User Jannette
by
6.2k points