To square a number in Ruby, the easiest way is to multiply the number by itself.

`square_num = num*num`

Here is a simple example using the code above.

```
square_of_10 = 10*10
print square_of_10
#Output
100
```

We can also use the Ruby **pow** method to square a number.

`square_num = num.pow(2)`

Using the pow method with a simple example.

```
square_of_10 = 10.pow(2)
#Output
100
```

Finally, we can find the square of a number in Ruby with the built-in exponent operator ******.

```
square_of_10 = 10**2
print square_of_10
#Output
100
```

Squaring numbers is a common task to do when working with numbers in a program. In Ruby, we can easily square a number.

By definition, the square of a number is the number multiplied by itself. We can calculate the square of a number in Ruby by multiplying it by itself.

Below are some examples of how to find the squares of various numbers in Ruby.

```
puts 4*4
puts 9*9
puts 13*13
puts 90*90
puts 2182*2182
#Output:
16
81
169
8100
4761124
```

Let’s dive into each one of these methods.

## Squaring a Number in Ruby with the pow Method

One calculation that is very easy to perform in Ruby is finding the square of a number. The power method also lets us compute the square of a number. The **pow** method takes two numbers as input, the first number is the base and the second number is the exponent. For squaring in Ruby, we pass “2” as the parameter in the **pow** method.

Below are some examples of how to use the **pow** method to find the squares of various numbers.

```
num1 = 4.pow(2)
num2 = 9.pow(2)
num3 = 13.pow(2)
num4 = 90.pow(2)
num5 = 2182.pow(2)
puts num1
puts num2
puts num3
puts num4
puts num5
#Output:
16
81
169
8100
4761124
```

## Finding the Square of a Number with the ** Operator in Ruby

We can also use the built-in ****** to find exponents in Ruby. To find a square with the built in exponent operator ****** , we just put “2” after ******.

Below are some examples of how to use the Ruby ****** operator to find the squares of different numbers.

```
puts 4**(2)
puts 9**(2)
puts 13**(2)
puts 90**(2)
puts 2182**(2)
#Output:
16
81
169
8100
4761124
```

Hopefully this article has been useful for you to learn how to square a number in Ruby.

## Leave a Reply