Created
April 18, 2022 17:19
-
-
Save caioertai/3e40d7b6ead5b20b338d0dd836c34e5f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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