generating the triangle numbers
I seem to always post mundane problems in comparison to the more exotic C++ programs on this forum. Not being one to disappoint, here's another problem.
I wrote a program that finds all the triangle numbers less than 100:
#include <iostream>
using namespace std;
int main() {
int a;
for(a = 1; a * ( a + 1) / 2 <= 100; a++){
cout << a * ( a + 1) / 2 << "\n";
}
return 0;
}
However, I would much rather avoid writing a * ( a + 1) / 2 <= 100 each time. I decided to define b = a * ( a + 1) / 2 <= 100.
#include <iostream>
using namespace std;
int main() {
int a = 1 , b;
for(a = 1; b <= 100; a++){
b = a * ( a + 1) / 2;
cout << b << "\n";
}
return 0;
}
Unfortunately, this doesn't work. All i've done is define b and replace it in the program that I know works.
Why is this second program not working, but the first one works perfectly?