6 Sum Square Difference

The sum of the squares of the first ten natural numbers is,

12 + 22 + ... + 10^2 = 385 The square of the sum of the first ten natural numbers is,

(1 + 2 + ... + 10)^2 = 552 = 3025 Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640.

Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.

#include <iostream>
#include <limits>

using namespace std;

void pause() { cin.ignore(numeric_limits<streamsize>::max(), '\n'); }

long int sumOfTheSquares()
{
    long int currentSum = 0;

    for (int i = 1; i <= 100; i++)
    {
        currentSum = currentSum + i*i;
    }
    return currentSum;
}

long long int squareOfTheSum()
{
    long int currentSum = 0;

    for (int i = 1; i <= 100; i++)
    {
        currentSum = currentSum + i;
    }

    long long int finalSum = currentSum*currentSum;
    return finalSum;
}

int main()
{
    long int a = squareOfTheSum();
    long long int b = sumOfTheSquares();

    cout << a - b;

    pause();
}

Solution: 25164150

Last updated