Write C# program to find sum of all elements of an array


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.  
  2. using System;
  3. class Program
  4. {
  5. static void Main()
  6. {
  7. int[] arr = new int[100];
  8. int i, num, sum = 0;
  9.  
  10. ////Reads size and elements in array
  11. Console.WriteLine("Enter size of the array: ");
  12. num = Convert.ToInt32(Console.ReadLine());
  13. Console.WriteLine("Enter "+num+" elements in the array: ");
  14.  
  15. for(i=0; i<num; i++)
  16. {
  17. arr[i] = Convert.ToInt32(Console.ReadLine());
  18. }
  19.  
  20. //Adding all elements
  21. for(i=0; i<num; i++)
  22. {
  23. sum = sum + arr[i]; // Calculating sum
  24. }
  25.  
  26. Console.WriteLine("Sum of all elements of array: "+sum);
  27.  
  28. Console.ReadLine();
  29. }
  30.  
  31. }

Result

C# language techstudy

     





Related Topics




Popular Posts