find the nth square number in csharp

To find the nth square number in C#, we can use the formula n^2. We can create a method that takes an integer n as input and returns the nth square number as output. Here is the code:

main.cs
public static int NthSquareNumber(int n)
{
    return n * n;
}
63 chars
5 lines

We can call this method with any value of n to get the corresponding square number. For example:

main.cs
int fifthSquare = NthSquareNumber(5);
// fifthSquare is now 25
63 chars
3 lines

Note that this method assumes that n is a non-negative integer. If n can be negative or a floating-point number, we may need to modify the method accordingly.

related categories

gistlibby LogSnag