Triangular number: The triangular numbers are those resulting from the sum of the first n whole numbers. So the first four are: 1; 3 (= 1 + 2); 6 (= 1 + 2 + 3) and 10 (= 1 + 2 + 3 + 4). Write the program to print out k first triangular.
Printable View
Triangular number: The triangular numbers are those resulting from the sum of the first n whole numbers. So the first four are: 1; 3 (= 1 + 2); 6 (= 1 + 2 + 3) and 10 (= 1 + 2 + 3 + 4). Write the program to print out k first triangular.
Have you done anything so far? Ask the user what k should be. After that you will probably want 2 loops to complete your task. One outer loop to go through each series up to k, and inside each loop you will want to print out the sum of 1 to whatever iteration of k you are on. Before you increment k be sure to print out the sum!
So roughly:
Code://...
int nK, sum;
std::cin >> nK;
for (int i = 0; i < nK; i++)
{
sum = 0;
for (int j = 1; j <= i + 1; j++)
sum += j;
std::cout << "Triangle Number " << i + 1<< " is: " << sum << std::endl;
}
//...
You only require one loop since you are computing all the other j - 1 triangular numbers for each inner loop.
That is all I will say since this is clearly a homework task.