Skip to content

Instantly share code, notes, and snippets.

@caioertai
Created April 18, 2022 17:19
Show Gist options
  • Save caioertai/3e40d7b6ead5b20b338d0dd836c34e5f to your computer and use it in GitHub Desktop.
Save caioertai/3e40d7b6ead5b20b338d0dd836c34e5f to your computer and use it in GitHub Desktop.
def calculation(first_number, second_number, operator)
case operator
when "+"
result = first_number + second_number
when "-"
result = first_number - second_number
when "*"
result = first_number * second_number
when "/"
result = first_number / second_number.to_f
end
result
end
require_relative "calculation"
# Receive two numbers and an operator and return the result
puts "Welcome to the calculator!"
puts "Give me a number"
number1 = gets.chomp.to_i
puts "Give me the operator"
operator = gets.chomp
puts "Give me another number"
number2 = gets.chomp.to_i
# calculation
result = calculation(number1, number2, operator)
# Print the result to the user
puts "Result of #{number1} #{operator} #{number2} is #{result}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment