This program takes two integers as input and store in a separate variables num1 and num2. After the calculation, the result will be stored in another variable sum and it will print the result.
This program will also calculate the minus/negative integer values Copy the below ruby program and execute it to see the real time output.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | =begin Ruby program to add two numbers. =end # 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}" |
Program Output
1 2 3 4 5 6 7 8 9 10 11 12 13 | First run: Enter first value: 123 Enter second value: 456 The sum is 579 Second run: Enter first value: -120 Enter second value: 20 The sum is -100 |
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.