A triangular number is a number of circles in an equilateral triangle evenly filled with circles. They’re called as triangular numbers because they can be made into triangular shapes.
Program
Write a program to calculate the sum of all the triangular numbers less than equal to given input number N.
Input
- The number N up to which triangular numbers have to be computed.
Output
- The sum of the all the triangular numbers.
Assumptions
- N is > 0
Algorithm
The first triangular number is formed by adding 1 to 0, then adding 2 to 1st triangular number, adding 3 to the 2nd triangular number and so on. If N = 25, the triangular numbers less than 25 are 1,3,6,10,15,21 and there sum is 1+3+6+10+15+21=57
Test cases
No | Input N (Upto to) | Intermediate (Triangular Numbers) | Output (Sum) |
---|---|---|---|
1 | 25 | 1, 3, 6, 10, 15, 21 | 57 |
2 | 50 | 1, 3, 6, 10, 15, 21, 28, 36, 45 | 166 |
3 | 100 | 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91 | 456 |
Instructions
- Accept the number N up to with (not including), we have to create the triangular numbers as input via the command line arguments.
- Write the logic to compute the triangle numbers and add them up.
- Display the resulting sum.