Ruby Program to Print Multiplication Table
You can print a multiplication table in Ruby using a loop like
1.upto(10) { |i| puts "#{i} x #{n} = #{i * n}" } where n is the number you want the table for.Examples
Inputn = 2
Output1 x 2 = 2
2 x 2 = 4
3 x 2 = 6
4 x 2 = 8
5 x 2 = 10
6 x 2 = 12
7 x 2 = 14
8 x 2 = 16
9 x 2 = 18
10 x 2 = 20
Inputn = 5
Output1 x 5 = 5
2 x 5 = 10
3 x 5 = 15
4 x 5 = 20
5 x 5 = 25
6 x 5 = 30
7 x 5 = 35
8 x 5 = 40
9 x 5 = 45
10 x 5 = 50
Inputn = 0
Output1 x 0 = 0
2 x 0 = 0
3 x 0 = 0
4 x 0 = 0
5 x 0 = 0
6 x 0 = 0
7 x 0 = 0
8 x 0 = 0
9 x 0 = 0
10 x 0 = 0
How to Think About It
To print a multiplication table, think of counting from 1 to 10 and multiplying each number by the chosen number. For each count, show the multiplication expression and the result. This repeats until the table is complete.
Algorithm
1
Get the number n for which to print the multiplication table2
Start a loop from 1 to 103
For each number i in the loop, calculate i multiplied by n4
Print the expression 'i x n = result'5
Repeat until the loop endsCode
ruby
puts "Enter a number to print its multiplication table:" n = gets.to_i 1.upto(10) do |i| puts "#{i} x #{n} = #{i * n}" end
Output
Enter a number to print its multiplication table:
5
1 x 5 = 5
2 x 5 = 10
3 x 5 = 15
4 x 5 = 20
5 x 5 = 25
6 x 5 = 30
7 x 5 = 35
8 x 5 = 40
9 x 5 = 45
10 x 5 = 50
Dry Run
Let's trace the program with input n = 3 through the code
1
Input number
User inputs 3, so n = 3
2
Start loop
Loop variable i starts at 1
3
Calculate and print
Print '1 x 3 = 3'
4
Next iteration
i becomes 2, print '2 x 3 = 6'
5
Continue until 10
Repeat until i = 10, printing each line
| i | Expression | Result |
|---|---|---|
| 1 | 1 x 3 | 3 |
| 2 | 2 x 3 | 6 |
| 3 | 3 x 3 | 9 |
| 4 | 4 x 3 | 12 |
| 5 | 5 x 3 | 15 |
| 6 | 6 x 3 | 18 |
| 7 | 7 x 3 | 21 |
| 8 | 8 x 3 | 24 |
| 9 | 9 x 3 | 27 |
| 10 | 10 x 3 | 30 |
Why This Works
Step 1: Getting input
The program asks the user for a number and stores it in n.
Step 2: Looping from 1 to 10
Using 1.upto(10) runs the loop 10 times, once for each multiplier.
Step 3: Printing each line
Inside the loop, it prints the multiplication expression and result using string interpolation.
Alternative Approaches
Using a for loop
ruby
puts "Enter a number:" n = gets.to_i for i in 1..10 puts "#{i} x #{n} = #{i * n}" end
This uses a for loop instead of upto; both are simple and readable.
Using times loop
ruby
puts "Enter a number:" n = gets.to_i 10.times do |i| puts "#{i + 1} x #{n} = #{(i + 1) * n}" end
Uses zero-based index, so we add 1 to i for correct multiplication.
Complexity: O(1) time, O(1) space
Time Complexity
The loop runs exactly 10 times, so time is constant O(1) regardless of input.
Space Complexity
No extra memory grows with input; space used is constant O(1).
Which Approach is Fastest?
All approaches run in constant time; choice depends on readability preference.
| Approach | Time | Space | Best For |
|---|---|---|---|
| upto loop | O(1) | O(1) | Simple and readable |
| for loop | O(1) | O(1) | Familiar to many programmers |
| times loop | O(1) | O(1) | Zero-based indexing style |
Use string interpolation with #{ } to easily show calculations in output.
Forgetting to convert input to integer with to_i causes errors in multiplication.