-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculator.rb
89 lines (53 loc) · 1.31 KB
/
calculator.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# calculator 1
# puts "Enter first number: "
# num1 = gets.chomp.to_i
# puts "Enter second number: "
# num2 = gets.chomp.to_i
# answer1 = num1 + num2
# answer2 = num1 - num2
# answer3 = num1 * num2
# answer4 = num1 / num2
# puts "Answer = #{answer1}"
# puts "Answer = #{answer2}"
# puts "Answer = #{answer2}"
# puts "Answer = #{answer4}"
# using conditionals -> calculator 2
# puts "Enter number1: "
# num1 = gets.chomp.to_i
# puts "Enter operator: "
# operator = gets.chomp.to_sym
# puts "Enter number2: "
# num2 = gets.chomp.to_i
# result = if operator == :+
# num1 + num2
# elsif operator == :-
# num1 - num2
# elsif operator == :*
# num1 * num2
# elsif operator == :%
# num1 % num2
# end
# puts "Your answer is #{result}"
# use case conditionals
# require 'pry'
puts "Enter the input"
input = gets.chomp.split(" ")
num1 = input[0].to_i
num2 = input[2].to_i
operator = input[1].to_sym
answer = case operator
when :+
num1 + num2
when :-
num1 - num2
when :*
num1 * num2
when :/
num1 / num2
when :%
num1 % num2
else
puts "Invalid operator"
end
puts "Your answer is: #{answer}"
# binding.pry