find the nth heptagonal number in csharp

To find the nth heptagonal number, we can use the formula:

H(n) = n(5n-3)/2

where n is the index of the heptagonal number starting from 1.

We can implement this formula in C# as follows:

main.cs
public static int GetHeptagonalNumber(int n)
{
    return n * (5 * n - 3) / 2;
}
81 chars
5 lines

This method takes an integer n as input and returns the corresponding nth heptagonal number.

We can then call this method to find the nth heptagonal number as follows:

main.cs
int n = 10;
int heptagonalNumber = GetHeptagonalNumber(n);
Console.WriteLine($"The {n}th heptagonal number is {heptagonalNumber}");
132 chars
4 lines

This will output:

main.cs
The 10th heptagonal number is 145
34 chars
2 lines

gistlibby LogSnag