Author Topic: C# - Sum, Average, Min and Max for N numbers  (Read 10763 times)

kathir

  • Administrator
  • Sr. Member
  • *****
  • Posts: 283
C# - Sum, Average, Min and Max for N numbers
« on: December 02, 2011, 02:05:42 pm »
Could you please help me out in C# that accepts N numbers as input and compute the following values:

1)total
2)average
3)minimum
4)maximum

Code: (C#)
//Source Code Listing


using System;
using System.Text;

namespace Min_Max_Average_Sum
{  
   class Program
   {
      static void Main(string[] args)
      {
         Console.Write("Enter the total numbers:");
         int n = Convert.ToInt32(Console.ReadLine());
         int[] arrNumbers = new int[n];

         for (int i = 0; i < n; i++)
         {
            Console.Write("Enter {0} No.: ", i + 1);
            arrNumbers[i] = Convert.ToInt32(Console.ReadLine());
         }

         int min = arrNumbers[0];
         int max = arrNumbers[0];
         int sum = 0;
         double average = 0;
         for (int i = 0; i < n; i++)
         {
            sum += arrNumbers[i];
            if (min > arrNumbers[i])
               min = arrNumbers[i];
            if (max < arrNumbers[i])
               max = arrNumbers[i];
         }
         average = (double)sum / n;

         Console.WriteLine();
         Console.WriteLine("Sum\t: {0}", sum);
         Console.WriteLine("Average\t: {0,2:n}", average);
         Console.WriteLine("Min\t: {0}", min);
         Console.WriteLine("Max\t: {0}", max);
      }      
   }
}

/***
The output is given below:
Enter the total numbers:5
Enter 1 No.: 100
Enter 2 No.: 200
Enter 3 No.: 101
Enter 4 No.: 201
Enter 5 No.: 501

Sum     : 1103
Average : 220.60
Min     : 100
Max     : 501
Press any key to continue . . .

***/



 


Disclaimer: This web site is for educational and informational purposes only. Click here to read the Disclaimer.
Content copyright 2010-2014. Kathiresan. All rights reserved.
This page is using SMF version 2.0.1