Write C# Program to Find the Average Values of all the Array Elements


Introduction

I have used Visual Studio 2012 for debugging purpose. But you can use any version of visul studio as per your availability..


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

  1. using System;
  2. class Program
  3. {
  4. public void sumAverageElements(int[] arr, int size)
  5. {
  6.  
  7. int sum = 0;
  8. int average = 0;
  9. for (int i = 0; i < size; i++)
  10. {
  11. sum += arr[i];
  12. }
  13. average = sum / size;
  14. Console.WriteLine("Sum Of Array is : " + sum);
  15. Console.WriteLine("Average Of Array is : " + average);
  16. Console.ReadLine();
  17. }
  18. public static void Main(string[] args)
  19. {
  20. int size;
  21. Console.WriteLine("Enter the Size :");
  22. size = Convert.ToInt32(Console.ReadLine());
  23. int[] a = new int[size];
  24. Console.WriteLine("Enter the Elements of the Array : ");
  25. for (int i = 0; i < size; i++)
  26. {
  27. a[i] = Convert.ToInt32(Console.ReadLine());
  28. }
  29. int len = a.Length;
  30. Program p = new Program();
  31. p.sumAverageElements(a, len);
  32. }
  33.  
  34. }

Result

C# language techstudy

     





Related Topics




Popular Posts