Saturday, September 28, 2013

Find Sum and Average an Array

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

namespace SumAndAverage
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] num=new int[500];
            int negativesum=0, positivesum=0, total=0,avg=0;

            Console.WriteLine("Enter the total number of element in an Array:\t");
            int n = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Enter the value one by one");
            for (int i = 0; i < n; i++)
            {
                num[i] = Convert.ToInt32(Console.ReadLine());
            }

            for (int i = 0; i < n; i++)
            {
                if (num[i] < 0)
                {
                    negativesum += num[i];
                }
                else if (num[i] > 0)
                {
                    positivesum += num[i];
                }
                else if(num[i]==0)
                {
                    ;
                }
                total += num[i];
                
            }
            avg = total / n;

            Console.WriteLine("The Positive Sum Value:=" + positivesum);
            Console.WriteLine("The Negative Sum Value:=" + negativesum);
            Console.WriteLine("The Sum of Array Value:=" + total);
            Console.WriteLine("The Average of Array Value:=" + avg);
            Console.ReadLine();
        }
    }
}

Output

Find Sum and Average an Array


No comments:

Post a Comment