nolw38 nolw38 - 16 days ago 5
C++ Question

syntax - c++ - Cant make my code works correctly

I'm trying to make my program calcul all these multiplications:
999*999 , 999*998, 998*998, 998*997, ......... Until 100*100.

Right now, it only calcul 999*999 998*998 997*997 ... 100*100.
I don't get why? Can you take a look on my code?
Thanks
BR

#include <iostream>
#include <vector>
#include <cmath>


int main () {
int i = 999;
int j = 999;
while (j >= 100) {
i == j ;
while (i >= j-1) {
std::cout << i*j << std::endl;
i -= j;
}
j = j-1;
}

return 0;
}

Answer

You are not seeing your loops correctly. Try to write the numbers you want to compute in a table first, and use it to build your loops.

For one value of your first loop variable - call it i, you want it multiplied by one, then two, then three (etc), values of j.

Regardless of what those values of j actually are, your loops should look like:

for(int i=999; i>=100; --i)
     for(int j=999; j>=i; j--)
           ; //computation goes here

Here you clearly see that for one value of i, you will use one value of j when i=999, then two values of j, then thre...

If you are new at coding, I would recommend starting with for loops and switch to while when you feel comfortable with the former ones.