Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Arithmetic Operations, Function Calls, Recursion and more in Ruby
''' Ruby is a language that is pretty similar to Python. If you know python then you can easily get a grasp of this language. You will be "Fast as Fuck boiii". For me it took me 1-2 hours to get a grasp of this languages. You can do this. This program demonstrates stuff like: Getting user input, Function declaration, Class declaration, Recursion, and a bit of String Formatting. Hope you will understand this program. Thanks for reading. PROGRAM BY \'JAMES COLLINS\' ''' class Arithmetic # This is class... I don't know what else to say. def add(num1, num2) # Function to add the given numbers. return num1 + num2 end def diff(num1, num2) # Function to find the difference between the given numbers and return their absolute value. return (num1 - num2).abs end def multi(num1, num2) # Function to find the product of the given numbers. return num1 * num2 end def div(num1, num2) # Function to divide the 2 given numbers and return the quotient as a float. return (num1 / (num2 * 1.0)) end def factorial(num) # Function to return the factorial of the given numbers. if num == 0 return 1 elsif num >= 1 return num * factorial(num - 1) elsif num < 0 return "\'Doesn't exist.\'" end end end num1 = gets.to_i # Getting the user input and converting it to int. num2 = gets.to_i # Getting the second number as string and converting it to int. obj = Arithmetic.new # Initialising the a new "Arithmetic" object. puts("Sum = #{obj.add num1, num2 }") puts("Difference = #{obj.diff num1, num2 }") puts("Product = #{obj.multi num1, num2 }") puts("Quotient = #{obj.div num1, num2 }") puts("Factorial = #{obj.factorial num1 }") #PROGRAM BY 'JAMES COLLINS'
run
|
edit
|
history
|
help
0
ruby LRU cache with constant get/set
Hash and Symbols
Ruby Inheritence
IS_IP?
variables 1
code
Ruby
testing ruby
3.4.0 / 3.5.2 / 3.5.1 / 3.6.1 / 3.6.5.#
Adiel Vasquez