find the nth square number in ruby

To find the nth square number in ruby, you can use the following formula:

main.rb
n**2
5 chars
2 lines

where n is the nth term.

For example, to find the 5th square number, you would do:

main.rb
5**2 #=> 25
12 chars
2 lines

This code will calculate the 5th square number, which is 25.

Here's an example program that uses this formula to find the nth square number:

main.rb
def find_nth_square(n)
  n**2
end

# Test the function
puts find_nth_square(5) #=> 25
puts find_nth_square(10) #=> 100
119 chars
8 lines

In this program, the find_nth_square function takes an integer n as an argument, and calculates the nth square number using the formula n**2. The function then returns the result.

We test the function by calling it with various inputs, and printing the results using puts.

gistlibby LogSnag