Answer to Question #311884 in C# for Shamoun Amin

Question #311884

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.


1
Expert's answer
2022-03-15T04:51:53-0400
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 ...");
            }
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS