Write a class marks with three data members to store three marks. Write three member functions, set_marks() to input marks, sum() to calculate and return the sum and avg() to calculate and return average marks.
using System;
namespace ConsoleApp1
{
class Program
{
public class Marks
{
int Mark1, Mark2, Mark3;
public void set_marks(int mark1, int mark2, int mark3)
{
Mark1 = mark1;
Mark2 = mark2;
Mark3 = mark3;
}
public int sum()
{
return Mark1 + Mark2 + Mark3;
}
public float avg()
{
return ((float)Mark1 + Mark2 + Mark3) / 3;
}
}
static void Main(string[] args)
{
try
{
Console.WriteLine("Input 1st mark:");
string str = Console.ReadLine();
int mark1 = Convert.ToInt32(str);
Console.WriteLine("Input 2nd mark:");
str = Console.ReadLine();
int mark2 = Convert.ToInt32(str);
Console.WriteLine("Input 3rd mark:");
str = Console.ReadLine();
int mark3 = Convert.ToInt32(str);
Marks marks = new Marks();
marks.set_marks(mark1, mark2, mark3);
Console.WriteLine("Sum is: {0}", marks.sum());
Console.WriteLine("Avg is: {0:F2}", marks.avg());
}
catch
{
Console.WriteLine("Bad Mark, repeat ...");
}
}
}
}
Comments
Leave a comment