April 8, 2020

How to write a program to print sum and average number in array in C#

Write a program in C# Sharp to accept size numbers enter by user and find their sum and average.


How to write a program to print sum and average number in array in C#



Like user Enter size 5 then enter 5 time number

The sum of 5 number is 1+2+3+4+5 = 15

The Average is

15/5 = 3


Sample Solution


C# Sharp Code


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class program
{
    public void sumAverageElements(int[] arr, int size)
    {

        int sum = 0;
        int average = 0;
        for (int i = 0; i < size; i++)
        {
            sum += arr[i];
        }
        average = sum / size;
        Console.WriteLine("Sum Of Array is : " + sum);
        Console.WriteLine("Average Of Array is : " + average);
        Console.ReadLine();
    }
    public static void Main(string[] args)
    {
        int size;
        Console.WriteLine("Enter the Size :");
        size = Convert.ToInt32(Console.ReadLine());
        int[] a = new int[size];
        Console.WriteLine("Enter the Elements of the Array : ");
        for (int i = 0; i < size; i++)
        {
            a[i] = Convert.ToInt32(Console.ReadLine());
        }
        int len = a.Length;
        program pg = new program();
        pg.sumAverageElements(a, len);
    }
}

Sample Output

The Sum is: 15

The Avg is: 3

How to write a program to print sum and average number in array in C#




No comments:

Post a Comment