
In Ruby, you can perform various mathematical operations using arithmetic operators. Here's a list of the most common math operations in Ruby:
.jpg)
Addition (+)
The addition operator (+) is used to add two numbers together. For example:
x = 10
y = 5
z = x + y
puts z # Output: 15
Subtraction (-)
The subtraction operator (-) is used to subtract one number from another. For example:
x = 10
y = 5
z = x - y
puts z # Output: 5
Multiplication (*)
The multiplication operator (*) is used to multiply two numbers together. For example:
x = 10
y = 5
z = x * y
puts z # Output: 50
Division (/)
The division operator (/) is used to divide one number by another. For example:
x = 10
y = 5
z = x / y
puts z # Output: 2
Modulo (%)
The modulo operator (%) is used to find the remainder when one number is divided by another. For example:
x = 10
y = 3
z = x % y
puts z # Output: 1
Exponentiation (**)
The exponentiation operator (**) is used to raise a number to a power. For example:
x = 2
y = 3
z = x ** y
puts z # Output: 8
Conclusion
By using these math operations and combining them with other Ruby programming concepts, you can perform complex calculations and build powerful programs.
0 Comments