# How to do table of multiplication of number program in Ruby?

I solved table of multiplication from 1 -10 program like follows:

x = 1
while x <= 10
puts " #{x} * 1 = #{x}" " " " #{x} * 2 = #{x*2}" " " " #{x} * 3 =
#{x*3}"......" #{x} * 1 = #{x*10}"
x = x + 1
end

From above program I am getting output improper way.

My question is how can I get output in proper way.

Thank you.

···

--
Posted via http://www.ruby-forum.com/.

Jaimin Pandya wrote in post #1137467:

From above program I am getting output improper way.

My question is how can I get output in proper way.

For future reference, it also helps if you define "improper" and
"proper". For all we know you might want it all concatenated on a single
line.

···

--
Posted via http://www.ruby-forum.com/.

hi Jaimin Pandya,

x = 1
while x <= 10
puts "#{x} * 1 = #{x} , #{x} * 2 = #{x*2}, #{x} * 3 = #{x*3}......
#{x} * 1 = #{x*10}"
x = x + 1
end

Use the code above it's working.

···

--
Posted via http://www.ruby-forum.com/.

2.upto(9) { |a| 1.upto(9) {|b| print "%3d" % (a*b)} ; puts }

2 4 6 8 10 12 14 16 18
3 6 9 12 15 18 21 24 27
4 8 12 16 20 24 28 32 36
5 10 15 20 25 30 35 40 45
6 12 18 24 30 36 42 48 54
7 14 21 28 35 42 49 56 63
8 16 24 32 40 48 56 64 72
9 18 27 36 45 54 63 72 81

Is it 'proper' ?

···

--
Posted via http://www.ruby-forum.com/.

I want to do program that's output should formatted as below:

1 * 1 = 1 2 * 1 = 2 3 * 1 = 3 ----------- 10 * 1 = 10
1 * 2 = 2 2 * 2 = 4 3 * 2 = 6 ----------- 10 * 2 = 20

···

-------------------------------------------------------------
-------------------------------------------------------------
--------------------------------------------------------------

1 * 10 = 10 2 * 10 = 20 3 * 10 = 30 --------- 10 * 10 = 100

I am not getting output like above.

could anyone help me on this?

Thank you.

--
Posted via http://www.ruby-forum.com/.

I want to do program that's output should formatted as below:

1 * 1 = 1 2 * 1 = 2 3 * 1 = 3 ----------- 10 * 1 = 10
1 * 2 = 2 2 * 2 = 4 3 * 2 = 6 ----------- 10 * 2 = 20

···

-------------------------------------------------------------
-------------------------------------------------------------
--------------------------------------------------------------

1 * 10 = 10 2 * 10 = 20 3 * 10 = 30 --------- 10 * 10 = 100

I am not getting output like above.

could anyone help me on this?

Run this code, It may be as your's :

x = 1
while x<=10
(1..10).each do |n|
puts "\n" if n ==1
print "#{x}*#{n}=#{x*n}"+"\t"
end
x += 1
end

--
Posted via http://www.ruby-forum.com/.

Nice, compact and easy to understand.
You should probably use a '1.upto...' in the outer loop so the output
shows what is being multiplied.

From: Regis d'Aubarede <lists@ruby-forum.com>
To: ruby-talk@ruby-lang.org
Date: 02/21/2014 11:38 AM
Subject: Re: How to do table of multiplication of number program in

Ruby?

···

"ruby-talk" <ruby-talk-bounces@ruby-lang.org> wrote on 02/21/2014 11:38:23 AM:

Sent by: "ruby-talk" <ruby-talk-bounces@ruby-lang.org>

> 2.upto(9) { |a| 1.upto(9) {|b| print "%3d" % (a*b)} ; puts }
2 4 6 8 10 12 14 16 18
3 6 9 12 15 18 21 24 27
4 8 12 16 20 24 28 32 36
5 10 15 20 25 30 35 40 45
6 12 18 24 30 36 42 48 54
7 14 21 28 35 42 49 56 63
8 16 24 32 40 48 56 64 72
9 18 27 36 45 54 63 72 81

Is it 'proper' ?

--
Posted via http://www.ruby-forum.com/.

Raja gopalan wrote in post #1137469:

hi Jaimin Pandya,

x = 1
while x <= 10
puts "#{x} * 1 = #{x} , #{x} * 2 = #{x*2}, #{x} * 3 = #{x*3}......
#{x} * 1 = #{x*10}"
x = x + 1
end

Use the code above it's working. The problem in your code is: "......"

I tried this program and output came correct. But output is not come in
table form.

···

--
Posted via http://www.ruby-forum.com/.

Regis d'Aubarede wrote in post #1137513:

1.upto(9) { |a| 1.upto(9) {|b| print "%3d%s" % [(a*b),b==1? "|" : ""]} ;
puts "\n"+(a==1 ? '-'*30 : "")}
1| 2 3 4 5 6 7 8 9
------------------------------
2| 4 6 8 10 12 14 16 18
3| 6 9 12 15 18 21 24 27
4| 8 12 16 20 24 28 32 36
5| 10 15 20 25 30 35 40 45
6| 12 18 24 30 36 42 48 54
7| 14 21 28 35 42 49 56 63
8| 16 24 32 40 48 56 64 72
9| 18 27 36 45 54 63 72 81

Is it 'proper' ?

Yes, It is proper.

Which type of output I want? That I just posted with format of output.

Thank you.

···

--
Posted via http://www.ruby-forum.com/.

Matthew Kerwin wrote in post #1137468:

Jaimin Pandya wrote in post #1137467:

From above program I am getting output improper way.

My question is how can I get output in proper way.

For future reference, it also helps if you define "improper" and
"proper". For all we know you might want it all concatenated on a single
line.

Thank you.

···

--
Posted via http://www.ruby-forum.com/.

Jaimin Pandya wrote in post #1138537:

I want to do program that's output should formatted as below:

1 * 1 = 1 2 * 1 = 2 3 * 1 = 3 ----------- 10 * 1 = 10
1 * 2 = 2 2 * 2 = 4 3 * 2 = 6 ----------- 10 * 2 = 20
-------------------------------------------------------------
-------------------------------------------------------------
--------------------------------------------------------------

1 * 10 = 10 2 * 10 = 20 3 * 10 = 30 --------- 10 * 10 = 100

I am not getting output like above.

could anyone help me on this?

Thank you.

Run this code, It may be as your's :

x = 1
while x<=10
(1..10).each do |n|
puts "\n" if n ==1
print "#{x}*#{n}=#{x*n}"+"\t"
end
x += 1
end

···

--
Posted via http://www.ruby-forum.com/.

Jaimin Pandya wrote in post #1138537:

I want to do program that's output should formatted as below:

1 * 1 = 1 2 * 1 = 2 3 * 1 = 3 ----------- 10 * 1 = 10
1 * 2 = 2 2 * 2 = 4 3 * 2 = 6 ----------- 10 * 2 = 20
-------------------------------------------------------------
-------------------------------------------------------------
--------------------------------------------------------------

1 * 10 = 10 2 * 10 = 20 3 * 10 = 30 --------- 10 * 10 = 100

I am not getting output like above.

could anyone help me on this?

Thank you.

Run also this code to get the thing which is same to your's thing.

1..10).each do |a|
(1..10).each do |b|
print "#{a}*#{b}=#{a*b}"+"\t"
end
end

···

--
Posted via http://www.ruby-forum.com/.

Thank you.

Run this code, It may be as your's :

x = 1
while x<=10
(1..10).each do |n|
puts "\n" if n ==1
print "#{x}*#{n}=#{x*n}"+"\t"
end
x += 1
end

Thank you very much.

···

--
Posted via http://www.ruby-forum.com/.

Run also this code to get the thing which is same to your's thing.

1..10).each do |a|
(1..10).each do |b|
print "#{a}*#{b}=#{a*b}"+"\t"
end
end

In above program, one code "puts "\n" if b == 1" is missing above this

code( print "#{a}*#{b}=#{a*b}"+"\t" ).

Thank you.

···

--
Posted via http://www.ruby-forum.com/.

("01".."99").each { |s| a,b=s.chars
print "%6s=%-2d" % [(c=("%s*%s"% [a,b])),eval(c)] if a!="0" && b!="0"
puts if b=="9"
}

···

--
Posted via http://www.ruby-forum.com/.