2

this is my code till now, but the answer is coming out to be incorrect. What am I doing wrong? I am supposed to find out the difference between the sum of squares, and square of sum of first 100 natural numbers.

#include<iostream>
using namespace std;

int main(){
    int sumOfSquare = 0;
    for(int i=1; i<=100; i++){
        i = i*i;
        sumOfSquare += i;
    };

    int squareOfSum = 0;
    for(int i; i<=100; i++){
        squareOfSum +=i;
    };

    squareOfSum = squareOfSum * squareOfSum;

    int difference = squareOfSum - sumOfSquare;

    cout<<difference;
}
  • 3
    `i = i*i;` modifies `i` and compromises the loop. – Evg Oct 27 '19 at 07:07
  • About [using namespace std](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – and you'll find quite a number of questions here on SO where it actually *did* lead to problems due to name clashes... – Aconcagua Oct 27 '19 at 07:47

3 Answers3

2

As Evg suggests, you can't modify your looping/indexing variable without modifying how many times your loop will run.

Try adding another variable for the "temporary" (per-iteration) sum, then use that. Like this:

#include<iostream>
using namespace std;

int main(){
    int sumOfSquare = 0;
    for(int i=1, j; i<=100; i++){
        j = i*i;
        sumOfSquare += j;
    };

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

    squareOfSum = squareOfSum * squareOfSum;

    int difference = squareOfSum - sumOfSquare;

    cout<<difference;
}

Edit: per Evg again, you did not initialize i on the second loop, so its initial value was undefined and it was looping an undefined amount of times. Add int i = 1 to the second loop (fixed in my code).

jkeys
  • 3,803
  • 11
  • 39
  • 63
2

Both loops of yours have a problem. First has i = i * i which is causing loop to not act the way you wanted, the second one uses i without initializing it. Even if you fix those, your code is very inefficient. You can make its efficiency O(1) by using a little math:

#include<iostream>

int main() {
    constexpr int n = 100;
    int sumOfSquare = n * (n + 1) * (2 * n + 1) / 6;  // sum of square of first n numbers
    int squareOfSum = n * (1 + n) / 2;                // sum of first n numbers
    squareOfSum = squareOfSum * squareOfSum;

    int difference = squareOfSum - sumOfSquare;

    std::cout << difference << '\n';
}
Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
0

Firstly when you are calculating sum of square in the first loop you are squaring in i but for next iteration of for loop the value would be different, suppose i have value 3 so in suqaring i=i*i; the value of i becomes 9 so in next iteration increment operator i++ would increase in actually 9 not in 3, so try to calculate square in other variable than i and then add it to sumofsquare in every iteration.

Secondly when you are using second first for loop to calculate sum of square, after completion of loop at that time back end in memory variable (i) have some value stored in it. now in second for loop when you calculate square of sum you do not initialized i to 1 how ever i have already some value in memory of 1st for loop. so in second loop initialize variable (i) to (1) so that it would calculate the squareofsum from 1 to 100.

#include<iostream>
using namespace std;

int main()
{
    int sumOfSquare = 0; int add=0;
    for(int i=1; i<=100; i++)
    {
        add = i*i;
        sumOfSquare += add;
    };

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

    squareOfSum = squareOfSum * squareOfSum;

    int difference = squareOfSum - sumOfSquare;

    cout<<difference;
}
Zain Ahmad
  • 30
  • 5