To find the nth triangular number, we can use a simple formula:
nthTriangularNumber = n * (n + 1) / 2
where n
is the given index of the triangular number.
Here's an implementation in C# using a loop to handle a case where n
is less than or equal to 0:
main.cs252 chars16 lines
We can simplify this further using the formula mentioned earlier:
main.cs174 chars10 lines
Now we can call this function with any positive integer n
to get the nth
triangular number.
gistlibby LogSnag