usingSystem;
classProgram
{
class Subject
{
private intmark;
private stringname;
publicSubject(string name, intmark)
{
this.name= name;
this.mark= mark;
}
public intGetMark()
{
returnmark;
}
public voidPrintSubject()
{
Console.WriteLine("|{0, -10} | {1, 10} |", name,mark);
}
}
// Student details
static stringname;
static int no;
static Subject[]subjects = newSubject[5];
static intnumOfSub = 0;
//By value method
public static voidAddSubject(string name,intmark)
{
subjects[numOfSub] = newSubject(name,mark);
numOfSub++;
}
public static floatAverage()
{
intsum = 0;
for (int i = 0; i <numOfSub; i++)
{
sum =sum + subjects[i].GetMark();
}
return (float)sum / numOfSub;
}
public static voidDisplay()
{
Console.WriteLine("---------------------------");
Console.WriteLine("|Student {0, -7} no. {1, 3} |",name, no);
for (int i = 0; i <numOfSub; i++)
{
Console.WriteLine("---------------------------");
subjects[i].PrintSubject();
}
Console.WriteLine("---------------------------");
Console.WriteLine("|Average: {0, 6} |",Average());
Console.WriteLine("---------------------------");
}
public static voidMain()
{
Console.Write("Enteryour name: ");
name =Console.ReadLine();
Console.Write("Enterroll no: ");
no = int.Parse(Console.ReadLine());
// The subjects arereaded.
stringsubjectName;
intsubjectMark;
Console.Write("Entersubject name: ");
subjectName =Console.ReadLine();
Console.Write("Entersubject mark: ");
subjectMark = int.Parse(Console.ReadLine());
AddSubject(subjectName,subjectMark);
Console.Write("Entersubject name: ");
subjectName =Console.ReadLine();
Console.Write("Entersubject mark: ");
subjectMark = int.Parse(Console.ReadLine());
AddSubject(subjectName,subjectMark);
Display();
}
}
Comments
Leave a comment