Tuesday, July 27, 2010

C# program for Structure and Enumeration


using System;
using System.Collections.Generic;
using System.Text;

namespace structenumexample
{
class Program
{
enum Sem
{
Nil=0, First = 1, Second= 2,Third = 3, Fourth = 4, Fifth = 5, Sixth = 6
}
struct stud
{
int regno, m1, m2, m3, m4, m5, tot;
string name, result, perf;
float avg;
Sem sem;

public stud(int regno, int m1, int m2, int m3, int m4, int m5,string name,int sem)
{
this.regno = regno;
this.m1 = m1;
this.m2 = m2;
this.m3 = m3;
this.m4 = m4;
this.m5 = m5;
this.name = name;
this.avg = 0;
this.tot = 0;
this.result = "";
this.perf = "";
this.sem = (Sem) sem;
}

public void proce()
{
tot = m1 + m2 + m3 + m4+m5;
avg = tot / 5;
if ((m1 >= 40) && (m2 >= 40) && (m3 >= 40) && (m4 >= 40) && (m5 >= 40))
{
result = "PASS";
if (avg > 75)
perf = "Distinction";
else if (avg > 60)
perf = "First";
else if (avg > 50)
perf = "Second";
else
perf = "Third";
}
else
{
result = "FAIL";
perf = "Nil";
}

}
public void showres()
{
System.Console.WriteLine(" {0} {1} {2} {3} {4} {5} {6} {7} {8} {9} {10} {11}", regno, name, sem,m1, m2, m3, m4, m5, tot, avg, result, perf);
}
}
public static void Main(string[] args)
{
int regno, m1, m2, m3, m4, m5;
string name;
int sem;
stud[] st = new stud[10];
int i;
for (i = 0; i < 10; i++)
{
Console.WriteLine("Enter the Regno of the student");
regno = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter the Name of the student");
name = Console.ReadLine();
Console.WriteLine("Enter the Semester of the student (1/2/3/4/5/6)");
sem = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter the m1 of the student");
m1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter the m2 of the student");
m2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter the m3 of the student");
m3 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter the m4 of the student");
m4 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter the m5 of the student");
m5 = Convert.ToInt32(Console.ReadLine());
st[i] = new stud(regno,m1,m2,m3,m4,m5,name,sem);
}
for (i = 0; i < 10; i++)
{
st[i].proce();
st[i].showres();
}
Console.ReadLine();
}
}
}

The above program is the example for the Structure and Enumeration usage in C#.

No comments:

Post a Comment