Forum Discussion

new2code's avatar
new2code
Copper Contributor
Aug 05, 2025

C# code

Hello everyone I was following along with the guided project calculate and print students grade. I know there are multiple was to code a solution but i was wondering why they chose to code the way they did and whether their way is better (functionally and readability) than the way I placed my code. I am very new to coding so any advice would be greatly appreciated.

 

Here is their code:

// initialize variables - graded assignments 

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;

// Adding each students grade together
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;

// Getting each students averages then converting to decimal
decimal sophiaScore = (decimal) sophiaSum / currentAssignments;
decimal nicolasScore = (decimal) nicolasSum / currentAssignments;
decimal zahirahScore = (decimal) zahirahSum / currentAssignments;
decimal jeongScore = (decimal) jeongSum / currentAssignments;

// Output into two columns followed by students names, grades, letter grades
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");

Here is my code: 

// initialize variables - graded assignments 
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;

// assigning students names to variables along with the header ex. "Student Grade"
string a = "Student        Grade\nSophia\t\t";
string b = "Nicolas\t\t";
string c = "Zahirah\t\t"; 
string d = "Jeong\t\t";

// Casting to a decimal
decimal sophiaGrade = (decimal) (sophia1 + sophia2 + sophia3 + sophia4 + sophia5) / currentAssignments;
decimal nicolasGrade = (decimal) (nicolas1 + nicolas2 + nicolas3 + nicolas4 + nicolas5) / currentAssignments;
decimal zahirahGrade = (decimal) (zahirah1 + zahirah2 + zahirah3 + zahirah4 + zahirah5) / currentAssignments;
decimal jeongGrade = (decimal) (jeong1 + jeong2 + jeong3 + jeong4 + jeong5) / currentAssignments;

// Combining students names and grades for final format
string cS = a + sophiaGrade;
string cN = b + nicolasGrade;
string cZ = c + zahirahGrade;
string cJ = d + jeongGrade;

// Outputs students names and Grades
Console.Write(cS + "\tA\n" + cN + "\tB\n" + cZ + "\tB\n" + cJ + "\tA");

 

No RepliesBe the first to reply

Resources