Home » Ruby programming » Ruby programs

# Ruby program to print multiplication table of a number

**Printing multiplication table**: Here, we are going to learn **how to print a multiplication table of a given number in Ruby programming language?**

Submitted by Hrithik Chandra Prasad, on August 21, 2019

## Printing multiplication table

This requires a very simple logic where we only have to multiply the number with digits from 1 to 10. This can be implemented by putting the multiplication statement inside a loop. We have mentioned two ways: one is by using while loop and the second one is by making use of for loop. When you are using while loop, first you will have to initialize i with 1 and increment it by 1 inside the loop. for loop, the method is simpler as it only requires the specification of for keyword along with the range on which the loop is going to work.

**Methods used:**

- puts: This is a predefined method which is used to put the string on the console.
- gets: This is also a predefined method in Ruby library which is used to take input from the user through the console in the form of string.
- *: This is an arithmetic operator commonly known as multiplication operator which takes two arguments and process them by giving out their product as a result.

**Variables used:**

- num: This variable is used to store the integer provided by the user.
- mult: This is storing the result for i*num.
- i: This is a loop variable which is initialized by 1.

### Ruby code to print multiplication table of a number

=begin Ruby program to print multiplication table of a number(by using for loop) =end puts "Enter the number:" num=gets.chomp.to_i for i in 1..10 mult=num*i puts "#{num} * #{i} = #{mult}" end

**Output**

Enter the number: 13 13 * 1 = 13 13 * 2 = 26 13 * 3 = 39 13 * 4 = 52 13 * 5 = 65 13 * 6 = 78 13 * 7 = 91 13 * 8 = 104 13 * 9 = 117 13 * 10 = 130

**Method 2:**

=begin Ruby program to print multiplication table of a number(by using while loop) =end puts "Enter the number:" num=gets.chomp.to_i i=1 while (i<=10) mult=num*i puts "#{num} * #{i} = #{mult}" i+=1 end

**Output**

Enter the number: 16 16 * 1 = 16 16 * 2 = 32 16 * 3 = 48 16 * 4 = 64 16 * 5 = 80 16 * 6 = 96 16 * 7 = 112 16 * 8 = 128 16 * 9 = 144 16 * 10 = 160

**Code explanation:**

The logic of code is pretty simple. In the first method, we are using while loop for the process and in the second one, we are using for loop. We have a variable mult in which we are multiplying the number with the i. The loop will terminate when i becomes equal to 10.

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.