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

1eb28f3c91b0a7b8bfb1a72cfa8befaa?d=identicon&s=25 Jaimin Pandya (jaimin)
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.
3df767279ce7d81db0a5bb30f5136863?d=identicon&s=25 Matthew Kerwin (mattyk)
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.
5dfc843f9ecf6fb64b52a35bdce43910?d=identicon&s=25 Raja gopalan (arc)
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: "......"
B078cb4f4fb473c7a54d1fc36d10c70e?d=identicon&s=25 Regis d'Aubarede (raubarede)
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' ?
920f6e4a0fbf997d851455f827a10ebc?d=identicon&s=25 unknown (Guest)
on 2014-02-21 17:51
(Received via mailing list)
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?
1eb28f3c91b0a7b8bfb1a72cfa8befaa?d=identicon&s=25 Jaimin Pandya (jaimin)
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.
1eb28f3c91b0a7b8bfb1a72cfa8befaa?d=identicon&s=25 Jaimin Pandya (jaimin)
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.
1eb28f3c91b0a7b8bfb1a72cfa8befaa?d=identicon&s=25 Jaimin Pandya (jaimin)
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.
1eb28f3c91b0a7b8bfb1a72cfa8befaa?d=identicon&s=25 Jaimin Pandya (jaimin)
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.
Cc7107cb48ea5e9d6b096f4c7fdf2bb1?d=identicon&s=25 Selvag Ruby (selvag1005)
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
Cc7107cb48ea5e9d6b096f4c7fdf2bb1?d=identicon&s=25 Selvag Ruby (selvag1005)
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
Cc7107cb48ea5e9d6b096f4c7fdf2bb1?d=identicon&s=25 Selvag Ruby (selvag1005)
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
1eb28f3c91b0a7b8bfb1a72cfa8befaa?d=identicon&s=25 Jaimin Pandya (jaimin)
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.
1eb28f3c91b0a7b8bfb1a72cfa8befaa?d=identicon&s=25 Jaimin Pandya (jaimin)
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.
B078cb4f4fb473c7a54d1fc36d10c70e?d=identicon&s=25 Regis d'Aubarede (raubarede)
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
}
07405f51053bf5f4e53b8c8ec00c4b24?d=identicon&s=25 Pranay reddy Sallaram (pranayreddy)
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
Bfc41f8b49d9d9fd40cfeb318f0b5512?d=identicon&s=25 Abishesh M. (abishesh_m)
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
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.