Problem

Problem Link

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

.

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

.

Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is .

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

Solution

CodeCPP fileDownload

#include <iostream>
 
int sum_of_square(int n) {
  int sum = 0;
  for (int i = 1; i <= n; ++i) {
    sum += i * i;
  }
  return sum;
}
 
int square_of_sum(int n) {
  int sum = 0;
  for (int i = 1; i <= n; ++i) {
    sum += i;
  }
  return sum * sum;
}
 
int main(void) {
  std::cout << square_of_sum(100) - sum_of_square(100) << std::endl;
  return 0;
}