Open In App

Ruby Integer magnitude function with example

The magnitude() function in Ruby returns the absolute value of the integer. It is similar to the abs function and is an alias of it.

Syntax: (number).magnitude



Parameter: The function takes the integer whose magnitude is to be returned.

Return Value: The function returns the absolute value of the integer.



Example #1:




# Ruby program of Integer magnitude function
  
# Initializing the numbers 
num1 = -21
num2 = 21 
num3 = 0 
num4 = -100 
  
# Printing the magnitude   value of integers 
puts (num1).magnitude  
puts (num2).magnitude  
puts (num3).magnitude  
puts (num4).magnitude  

Output:

21
21
0
100

Example #2:




# Ruby program of Integer magnitude function
  
# Initializing the numbers 
num1 =29
num2 = -7
num3 = 90
num4 = -10
   
      
# Printing the magnitude value of integers 
puts (num1).magnitude  
puts (num2).magnitude  
puts (num3).magnitude  
puts (num4).magnitude   

Output:

29
7
90
10
Article Tags :