0
I’m a beginner in programming and I’m struggling with the following problem:
Make a program that calculates and prints all triangular numbers smaller than 1000.
A triangular number is a natural number which can be represented in equilateral triangle shape. To find the nth number triangular from the previous one just add n units. A sequence of triangular numbers (sequence A000217 in OEIS), starting with the 0 th term, is:
0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...
My code went like this:
#include<iostream>
using namespace std;
int main(){
int b, a;
for (a = 0, b = 0; b < 1000;a++){
b = (a + 1) + b;
cout << b << endl;
}
return 0;
}
The only problem is that when I compile, it goes up to 1035.