Ruby program to add two integer numbers

Adding two numbers in Ruby

To add numbers in ruby we will use below methods

puts: This method is used to display some message to the user.
gets: This method is used to take input from the user.
.to_i: Convert String to Integer.
+: It is a mathematical operator which accepts two numerical parameters and returns a numerical value. A binary operator to add two values

# input the numbers and converting 
# them into integer 
puts "Enter first value: "
num1=gets.chomp.to_i
puts "Enter second value: "
num2=gets.chomp.to_i

# finding sum 
sum=num1+num2

# printing the result
puts "The sum is #{sum}"

Output

First run:
Enter first value:
123
Enter second value:
234
The sum is 357

Second run:
Enter first value:
-123
Enter second value:
100
The sum is -23

 


Flutter Questions

Android Questions

Python Questions

Java Questions