w3resource
C# Sharp Programming Exercises

C# Sharp Basic Algorithm Exercises: Compute the sum of the elements of an given array of integers


C# Sharp Basic Algorithm: Exercise-91 with Solution

Write a C# Sharp program to compute the sum of the elements of an given array of integers.

Pictorial Presentation:

C# Sharp: Basic Algorithm Exercises - Compute the sum of the elements of an given array of integers.

Sample Solution:-

C# Sharp Code:

using System;
namespace exercises
{
   class Program
    {       
        static void Main(string[] args)
        {
            Console.WriteLine(test(new[] { 10, 20, 30, 40, 50 }));
            Console.WriteLine(test(new[] { 10, 20, -30, -40, 50 })); 
        }     
       public static int  test(int[] a1)
          {
            return a1[0] + a1[1] + a1[2] + a1[3] + a1[4];
          } 
   }
} 

Sample Output:

150
10

Flowchart:

C# Sharp: Flowchart: Compute the sum of the elements of an given array of integers.

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a C# Sharp program to check two given arrays of integers of length 1 or more and return true if they have the same first element or they have the same last element.
Next: Write a C# Sharp program to rotate the elemets of a given array of integers (length 4 ) in left direction and return the new array.

What is the difficulty level of this exercise?



New Content: Composer: Dependency manager for PHP, R Programming