A1 Write a program to find total and average of given marks in an array

    class Program
    {
        static void Main(string[] args)
        {
            int[] a = new int[5];
            int Sum, Avg;

            Console.WriteLine("Enter 5 Marks : ");
            for (int i = 0; i < a.Length; i++)
                a[i] = int.Parse(Console.ReadLine());

            Sum = 0;
            for (int i = 0; i < a.Length - 1; i++)
            {
                Sum += a[i];
            }
            Avg = Sum / a.Length;

            Console.WriteLine("Total Mark : " + Sum);
            Console.WriteLine("Average Mark : " + Avg);

            Console.ReadLine();
        }

    }

Output :

4 comments:

  1. why here taken i<a.length-1...?

    ReplyDelete
    Replies
    1. condition mentioned there was wrong

      Use this

      i<=a.Length-1

      or

      i<a.Length

      Delete
  2. there is no necessity of two for loops

    ReplyDelete
  3. Yes kartheekayan correct.

    But for understanding purpose I performed with separate loop

    ReplyDelete