Home » Ruby programming » Ruby programs

# Ruby program to add two integer numbers

**Adding two numbers in Ruby**: Here, we are going to learn **how to add two integer numbers in Ruby programming language?**

Submitted by Hrithik Chandra Prasad, on August 06, 2019

## Adding two numbers in Ruby

Given/Input two integer numbers and we have to find the addition of the integer numbers in Ruby.

In this problem, we will find out the way to **add two integers in Ruby**. Adding two numbers is not as easy as it seems because when we use gets method which stands for getting string, we get the value in the string. We will not get the appropriate result if we add those values.

**Methods used:**

**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**: When we take input through gets method, it is a string. For further calculations, it is mandatory to convert it into an integer for exact results and this method returns the integer conversion of the given string. **+**: It is a mathematical operator which accepts two numerical parameters and returns a numerical value. A binary operator to add two values.

**Variables used:**

**num1**: To store the first value**num2**: To store the second value**sum**: To store the sum i.e. result

## Ruby code to add two integer numbers

=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}"

**Output**

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

**Code explanation:**

This program is to **add two integer values and print the sum** of these entered numbers. It takes two integers as input and stored them to variables, num1 and num2. Then using the + (arithmetic operator) to store the sum of these variables into the sum and stores this sum and then the puts method prints the sum value with the print statement The sum is $sum.

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions

**Ad:**
Are you a blogger? Join our Blogging forum.