C# Student Grades: Format Console Output
Finish the student-grade report by adding a header and spacing the output into columns. This example uses newline and tab escape sequences.
Newlines and tab stops
\n inserts a newline inside a string. Console.WriteLine() also ends its output with a newline. \t advances to the next tab stop; its visible width depends on the terminal and the preceding text.
Complete report program
int currentAssignments = 5;
int sophia1 = 93;
int sophia2 = 87;
int sophia3 = 98;
int sophia4 = 95;
int sophia5 = 100;
int nicolas1 = 80;
int nicolas2 = 83;
int nicolas3 = 82;
int nicolas4 = 88;
int nicolas5 = 85;
int zahirah1 = 84;
int zahirah2 = 96;
int zahirah3 = 73;
int zahirah4 = 85;
int zahirah5 = 79;
int jeong1 = 90;
int jeong2 = 92;
int jeong3 = 98;
int jeong4 = 100;
int jeong5 = 97;
int sophiaSum = sophia1 + sophia2 + sophia3 + sophia4 + sophia5;
int nicolasSum = nicolas1 + nicolas2 + nicolas3 + nicolas4 + nicolas5;
int zahirahSum = zahirah1 + zahirah2 + zahirah3 + zahirah4 + zahirah5;
int jeongSum = jeong1 + jeong2 + jeong3 + jeong4 + jeong5;
decimal sophiaScore = (decimal)sophiaSum / currentAssignments;
decimal nicolasScore = (decimal)nicolasSum / currentAssignments;
decimal zahirahScore = (decimal)zahirahSum / currentAssignments;
decimal jeongScore = (decimal)jeongSum / currentAssignments;
Console.WriteLine("Student\t\tGrade\n");
Console.WriteLine("Sophia:\t\t" + sophiaScore + "\tA");
Console.WriteLine("Nicolas:\t" + nicolasScore + "\tB");
Console.WriteLine("Zahirah:\t" + zahirahScore + "\tB");
Console.WriteLine("Jeong:\t\t" + jeongScore + "\tA");Expected values
| Student | Total | Assignments | Average | Given grade |
|---|---|---|---|---|
| Sophia | 473 | 5 | 94.6 | A |
| Nicolas | 418 | 5 | 83.6 | B |
| Zahirah | 417 | 5 | 83.4 | B |
| Jeong | 477 | 5 | 95.4 | A |
Spacing varies with tab settings, and numeric decimal separators follow the current culture. The letter grades here are supplied labels for this sample data, not calculated by the program. If you change the scores, update the labels or add explicit grade-boundary logic.
A more predictable alignment option
Interpolation alignment specifies a field width instead of relying on tab stops:
Console.WriteLine($"{"Student",-12} {"Score",5} Grade");
Console.WriteLine($"{"Sophia",-12} {sophiaScore,5:F1} A");In the second example, -12 left-aligns the name and 5:F1 right-aligns the score with one fractional digit.
Try it yourself
Use a longer student name and compare the tab-based layout with the field-width layout. Check that the totals and averages have not changed while you adjust presentation.
Continue learning
Further reading: Microsoft documentation.
Console alignment troubleshooting
Why do tab-separated columns look different in another terminal?
A tab advances to a tab stop rather than inserting a fixed number of spaces. Font, terminal settings and the length of the preceding text affect the visible alignment. Interpolation alignment sets a minimum field width, which is more predictable for a simple text report.
Practice and check
Try a student name longer than the existing names. Check both the tab-based output and the aligned version. A field wider than its minimum width is not truncated automatically, so the column may still expand.
Comments
Post a Comment