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
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.