# How to write a program in ruby for calculator by selecting option for operation. When i select 1 then the operation should be addition and so on

**URL:** <https://www.ruby-forum.com/t/how-to-write-a-program-in-ruby-for-calculator-by-selecting-option-for-operation-when-i-select-1-then-the-operation-should-be-addition-and-so-on/261828>\
**Category:** Development\
**Created:** [September 14, 2021, 8:03am UTC](https://www.ruby-forum.com/t/how-to-write-a-program-in-ruby-for-calculator-by-selecting-option-for-operation-when-i-select-1-then-the-operation-should-be-addition-and-so-on/261828 "2021-09-14T08:03:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![aniket](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c0e974/32.png) [@aniket](https://www.ruby-forum.com/u/aniket)\
**Post date:** [September 14, 2021, 8:03am UTC](https://www.ruby-forum.com/t/how-to-write-a-program-in-ruby-for-calculator-by-selecting-option-for-operation-when-i-select-1-then-the-operation-should-be-addition-and-so-on/261828/1 "2021-09-14T08:03:56Z")

</div>

```
puts "Enter a value: "
num1 = gets.chomp()
puts "Enter b value: "
num2 = gets.chomp()

sum = num1.to_f + num2.to_f #Addition
puts "sum = #{sum}" 
subtract = num1.to_f - num2.to_f #substraction
puts "subtract = #{subtract}" 
multiply = num1.to_f * num2.to_f #multiplication
puts "multiply = #{multiply}"
divison = num1.to_f / num2.to_f #division
puts "divison = #{divison}"
With the above program i need to add option in all the operation. When i select 1 it should add the numbers.
```

---

<div class="post-metadata">

**Author:** ![SouravGoswami](https://www.ruby-forum.com/user_avatar/www.ruby-forum.com/souravgoswami/32/2543_2.png) [@SouravGoswami](https://www.ruby-forum.com/u/SouravGoswami)\
**Post date:** [September 14, 2021, 8:25am UTC](https://www.ruby-forum.com/t/how-to-write-a-program-in-ruby-for-calculator-by-selecting-option-for-operation-when-i-select-1-then-the-operation-should-be-addition-and-so-on/261828/2 "2021-09-14T08:25:44Z")

</div>

Hi, I saw some basic problems in your code:

#### Use `STDIN.gets()` instead of `gets()`

`gets()` first checks the program arguments and read that as a file, it’s not what you want. For example:

 ![image](https://www.ruby-forum.com/uploads/default/original/2X/d/d77d2f1eb4d1d6ccd6fc1de8823683a6b344ba55.jpeg)

#### Don’t use chomp() and then `.to_f()`:

The `.to_f()` or `.to_i()` method converts string to integer. You can just run `STDIN.gets.to_f`, and it will work fine.

#### Don’t use Multiple same method calls:

Every method call has some cost added to it, in terms of CPU time. So why not just use `num = STDIN.gets.to_f` instead of using `.to_f()` everywhere? .to\_f converts string to float, and it wastes CPU time. Here we just used some memory to store it.

So your code now looks like this:

```auto
puts "Enter a value: "
num1 = STDIN.gets.to_f
puts "Enter b value: "
num2 = STDIN.gets.to_f

sum = num1 + num2 #Addition
puts "sum = #{sum}"
subtract = num1 - num2 #substraction
puts "subtract = #{subtract}"
multiply = num1 * num2 #multiplication
puts "multiply = #{multiply}"
divison = num1 / num2 #division
puts "divison = #{divison}"

```

* * *

#### Now coming to the original question.

How can you add 1 for addition, 2 for subtraction, 3 for multiplication and 4 for division? Well, that’s simple.

- Just ask the user to enter a number within 4
- clamp it to 4 so if the user enters anything above 4, like 66 it becomes 4. Also make sure the user can’t enter \< 1, so -100 becomes 1, too.
- Store the operations in an array, get items from the array (user\_input - 1).

For example:

```auto
OPERATIONS = %w(+ - * /)

print "Enter a value: "
num1 = STDIN.gets.to_f
print "Enter b value: "
num2 = STDIN.gets.to_f
print "Enter operation (1 +, 2 -, 3 *, 4 /): "
op = STDIN.gets.to_i.clamp(1, 4)

# Operation
operation = OPERATIONS[op - 1]

# Output as a floating point number
out_f = num1.method(operation).call(num2)

# Output as an integer
out_i = out_f.to_i

# Don't show 50.0 for example, when it's 50. Show 50.12 when it's 50.12.
result = out_i == out_f ? out_i : out_f.round(4)

# Format string
puts "#{num1} #{operation} #{num2} = #{result}"

```

Now it works like this:

```auto
$ ruby p.rb 
Enter a value: 30
Enter b value: 50
Enter operation (1 +, 2 -, 3 *, 4 /): 3
30.0 * 50.0 = 1500
$ ruby p.rb 
Enter a value: 30.23
Enter b value: 50
Enter operation (1 +, 2 -, 3 *, 4 /): 1
30.23 + 50.0 = 80.23

```

---

<div class="post-metadata">

**Author:** ![SouravGoswami](https://www.ruby-forum.com/user_avatar/www.ruby-forum.com/souravgoswami/32/2543_2.png) [@SouravGoswami](https://www.ruby-forum.com/u/SouravGoswami)\
**Post date:** [September 14, 2021, 8:32am UTC](https://www.ruby-forum.com/t/how-to-write-a-program-in-ruby-for-calculator-by-selecting-option-for-operation-when-i-select-1-then-the-operation-should-be-addition-and-so-on/261828/3 "2021-09-14T08:32:13Z")

</div>

If you directly want to use the operation, entered by the user, just use this:

```auto
# Operation
operation = OPERATIONS.include?(op) ? op : ?+

```

Replace the `Operation` section with this code. So if the user enters invalid operation, it will just use +.

And also, replace `op = STDIN.gets.strip`. Here `strip()` strips any `\t`, `\n`, `\s`, `\r`, `\u0000` (unicode), `\x00` (hex) and `\00` (octal). So it deletes multiple copies of them as well, both from the beginning and the end of the string!

So it will work like this:

```auto
$ ruby p.rb
Enter a value: 3
Enter b value: 5 
Enter operation (1 +, 2 -, 3 *, 4 /): *
3.0 * 5.0 = 15

$ ruby p.rb 
Enter a value: -2000
Enter b value: -1000
Enter operation (1 +, 2 -, 3 *, 4 /): +
-2000.0 + -1000.0 = -3000

```

---

<div class="post-metadata">

**Author:** ![aniket](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c0e974/32.png) [@aniket](https://www.ruby-forum.com/u/aniket)\
**Post date:** [September 14, 2021, 8:36am UTC](https://www.ruby-forum.com/t/how-to-write-a-program-in-ruby-for-calculator-by-selecting-option-for-operation-when-i-select-1-then-the-operation-should-be-addition-and-so-on/261828/4 "2021-09-14T08:36:21Z")

</div>

> [@SouravGoswami](#):
>
> `OPERATIONS.include?(op) ? op : ?+`

thank you for correcting my code and also answering my question. I understood the code.
