• Skip to primary navigation
  • Skip to main content

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
  • Write for Us
You are here: Home / Ruby / How to Square a Number in Ruby

How to Square a Number in Ruby

October 17, 2022 Leave a Comment

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.

Other Articles You'll Also Like:

  • 1.  Demystifying Square Brackets: Understanding the [] Notation
  • 2.  How to Print Without Newline in Ruby
  • 3.  Using Ruby to Reverse String
  • 4.  Ruby Round – Round Number to Nearest Integer with the round Method
  • 5.  Text to Speech Transformation: A Multilingual Adventure
  • 6.  How to Print With Newline in Ruby
  • 7.  Using Ruby to Print to Console
  • 8.  Ruby has_key Method – Check if Hash Has Key
  • 9.  Get the Sum of an Array in Ruby
  • 10.  Using Ruby to Create List of Prime Numbers

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2023 · The Programming Expert · About · Privacy Policy