How to sum the total values of an array of ints elements in C#

2 Answers

0 votes
using System;
using System.Linq;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arr = { 1, 2, 3, 4, 5 };

            int sum = arr.Sum();

            Console.WriteLine(sum);
        }
    }
}


/*
run:

15

*/

 



answered Mar 2, 2017 by avibootz
0 votes
using System;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arr = { 1, 2, 3, 4, 5 };
            int sum = 0;

            for (int i = 0; i < arr.Length; i++)
                sum += arr[i];

            Console.WriteLine(sum);
        }
    }
}


/*
run:

15

*/

 



answered Mar 2, 2017 by avibootz

Related questions

2 answers 269 views
1 answer 165 views
1 answer 187 views
8 answers 571 views
571 views asked Jan 21, 2017 by avibootz
2 answers 228 views
4 answers 325 views
1 answer 162 views
...