find the nth nonagonal number in csharp

One way to find the nth nonagonal number in C# is to use the formula (5 * n^2 - 3 * n) / 2, where n is the index of the nonagonal number starting from zero.

Here's an implementation of this formula in C#:

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

You can then call this method with the desired index n to get the corresponding nonagonal number. For example, to find the 5th nonagonal number, you can do:

main.cs
int fifthNonagonalNumber = GetNthNonagonalNumber(5);
53 chars
2 lines

This will give you the result of 70, which is the 5th nonagonal number.

gistlibby LogSnag