A very big sum code in C#

Problem source: https://www.hackerrank.com/challenges/a-very-big-sum

You are given an array of integers of size N. You need to print the sum of the elements of the array.

Note: A signed 32bit integer value uses 1st bit to represent the sign of the number and remaining 31bits to represent the magnitude. The range of the 32bit integer is 231 to 2311 or [2147483648,2147483647]. When we add several integer values, the resulting sum might exceed this range. You might need to use long long int in C/C++ or long data type in Java to store such sums.

Input Format

The first line of the input consists of an integer N. The next lines contain N space separated integers describing the array.

Constraints
1N10
0A[i]109

Output Format

Output a single value equal to the sum of the elements of the array.

Sample Input

5
1000000001 1000000002 1000000003 1000000004 1000000005

Sample Output

5000000015

class A_Very_Big_Sum_Hackerrank
{
static void Main(string[] args)
{
int T = Convert.ToInt16(Console.ReadLine());
Int64 total = 0;
foreach (string str in Console.ReadLine().Split(‘ ‘))
{
total += Convert.ToInt64(str);
}
Console.WriteLine(total);
}
}


Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s