Ruby Numbers Operations - ashish9342/FreeCodeCamp GitHub Wiki

Ruby Numbers Operations

In Ruby you can perform all standard math operations on numbers, including: addition +, subtraction -, multiplication *, division /, find remainders %, and work with exponents **.

Addition:

  • Numbers can be added together using the + operator.
15 + 25
# returns:
40

Subtraction:

  • Numbers can be subtracted from one another using the - operator.
25 - 15
# returns:
10

Multiplication:

  • Numbers can be multiplied together using the * operator.
10 * 5
# returns:
50

Division:

  • Numbers can be divided by one another using the / operator.
10 / 5
# returns:
2

Remainders:

  • Remainders can be found using the modulus % operator.
10 % 3
# returns:
1 # because the remainder of 10/3 is 1

Exponents:

  • Exponents can be calculated using the ** operator.
2 ** 3
# returns:
8 # because 2 to the third power, or 2 * 2 * 2 = 8
Home Next
Ruby Numbers Basics Ruby Numbers Methods
⚠️ **GitHub.com Fallback** ⚠️