Search code examples
c++arrayssum-of-digits

Sum of long integer array in c++


I know this question asked many times but I am facing different problem in my code, I try to calculate sum of long integers range between 2-15.

Code:

long array[20];
long NUMBERS;

cout << "How many numbers ? : ";
cin >> NUMBERS;
long sum=0;
for (int i = 0; i < NUMBERS;i++){

    cout << "Input number " << (i+1) << " : ";
    cin >> array[i];    
}

cout << "Calculate Sum" << endl;
for (int i = 0; i < NUMBERS;i++){
    sum = sum + array[i];
}

cout << "Sum is : " << sum << endl;

When I input these three numbers.

  1. 1234567
  2. 123456
  3. 12345

Output:

Sum is : 1370368

but actual answer is : 3703627.

I try these solutions summing-large-numbers and sum-of-alternate-elements-of-integer-array but still not get right solution, also how we can solve this problem if user input different number with different ranges.


Solution

  • This isn't about programming, but math... Hope this helps: http://www.wikihow.com/Add-Large-Numbers

    (As a simple example, add 1 and 11. What is the result? 12 or 21?)