Use a for statement

Difference of Squares
Difference of Squares in C#
public static class DifferenceOfSquares
{
    public static int CalculateSquareOfSum(int max)
    {
        var sum = 0;

        for (var i = 1; i <= max; i++)
            sum += i;

        return sum * sum;
    }

    public static int CalculateSumOfSquares(int max)
    {
        var sumOfSquares = 0;

        for (var i = 1; i <= max; i++)
            sumOfSquares += i * i;

        return sumOfSquares;
    }

    public static int CalculateDifferenceOfSquares(int max) =>
        CalculateSquareOfSum(max) - CalculateSumOfSquares(max);
}

Method: CalculateSquareOfSum()

The square of the sum is defined as: sum all numbers from 1 to the provided max, and then square that number.

Let's start by defining a variable to hold the sum:

var sum = 0;

We can then iterate over the number using a for statement and add each number to the sum:

for (var i = 1; i <= max; i++)
    sum += i;

The last step is to run the square of the sum:

return sum * sum;

Method: CalculateSumOfSquares()

The calculate the sum of the squares, we once again start out with creating an enumerable of the numbers from 1 to max:

Let's start by defining a variable to hold the sum:

var sumOfSquares = 0;

We can then iterate over the number using a for statement and add each number's square to the sum:

for (var i = 1; i <= max; i++)
    sumOfSquares += i * i;

The last step is to run the sum of the squares:

return sumOfSquares;

Method: CalculateDifferenceOfSquares()

The CalculateDifferenceOfSquares() method is nothing more than calling the two methods we just created:

public static int CalculateDifferenceOfSquares(int max) =>
    CalculateSquareOfSum(max) - CalculateSumOfSquares(max);
6th Nov 2024 · Found it useful?