Write a program in ruby to add two numbers

By: Prof. Dr. Fazal Rehman | Last updated: February 3, 2024

I am sharing with you, a simple program in Ruby to add two numbers:

Ruby

print “Enter the first number: “

FirstNo = gets.chomp.to_i

 

print “Enter the second number: “

SecondNo = gets.chomp.to_i

 

sum = FirstNo + SecondNo

puts “The sum of the two numbers is: #{sum}”

Step 1:  user is will enter two numbers

Step 2:  two numbers are stored in the variables and variables are FirstNo and SecondNo.

Step 3:  The to_i method is used to convert the input to an integer because input is initially a string.

Step 4:  the sum of the two numbers is calculated

Step 5:  sum of the two numbers is stored in the variable sum.

Step 6:  The puts method is used to print out the result.

All Copyrights Reserved 2025 Reserved by T4Tutorials