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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
on 2014-02-21 13:32
```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.```
on 2014-02-21 13:57
```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.

Add some commas.

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.```
on 2014-02-21 13:59
```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: "......"```
on 2014-02-21 17:38
```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' ?```
on 2014-02-21 17:51
```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.

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

> 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?```
on 2014-03-02 13:37
```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.```
on 2014-03-02 13:41
```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: "......"

Thank you for your reply.

I tried this program and output came correct. But output is not come in
table form.```
on 2014-03-02 13:44
```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.```
on 2014-03-02 13:45
```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.
>
> Add some commas.
>
> 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.```
on 2014-03-03 07:37
```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```
on 2014-03-03 07:38
```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```
on 2014-03-03 08:07
```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```
on 2014-03-04 01:50
```>> 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

I got desire output from your answer.

Thank you very much.```
on 2014-03-04 01:55
```> 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.```
on 2014-03-04 19:16
```("01".."99").each { |s| a,b=s.chars.map(&:to_i); c=a*b
print "%6s=%-2d" % ["%d*%d"% [a,b],c] if c> 0
puts if b==9
}```
on 2014-11-06 18:27
```if i give the input as num 5 ishould get the output as

5*1=5
5*2=10
5*3=15
5*4=20
5*5=25
5*6=30
5*7=35
5*8=40
5*9=45
5*10=50

i need an urgent reply```
on 2015-01-12 08:22
```Pranay reddy Sallaram wrote in post #1161867:
> if i give the input as num 5 ishould get the output as
>
> 5*1=5
> 5*2=10
> 5*3=15
> 5*4=20
> 5*5=25
> 5*6=30
> 5*7=35
> 5*8=40
> 5*9=45
> 5*10=50
>
> i need an urgent reply

class Ruby
def function
x=5
(1..10).each do |n|
puts "\n" if n ==1
print "#{x}*#{n}=#{x*n}"+"\n"
end
end
end
mail1 = Ruby.new
mail1.function

i think it may help you```
This topic is locked and can not be replied to.