find the nth hexagonal number in csharp

To find the nth hexagonal number in C#, we can use the formula:

main.cs
H(n) = n(2n-1)
15 chars
2 lines

Here is the implementation of the formula in C#:

main.cs
public static int GetHexagonalNumber(int n)
{
    return n * (2 * n - 1);
}
76 chars
5 lines

To use this method and get the nth hexagonal number, we can call it as follows:

main.cs
int n = 10; //nth term
int hexagonalNumber = GetHexagonalNumber(n);

Console.WriteLine(hexagonalNumber); //Output: 190
119 chars
5 lines

This should output 190, which is the 10th hexagonal number.

gistlibby LogSnag