I solved table of multiplication from 1 -10 program like follows:
x = 1
while x <= 10
puts " #{x} * 1 = #{x}" " " " #{x} * 2 = #{x2}" " " " #{x} * 3 =
#{x3}"…" #{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.
Jaimin P. 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.
hi Jaimin P.,
x = 1
while x <= 10
puts “#{x} * 1 = #{x} , #{x} * 2 = #{x2}, #{x} * 3 = #{x3}…
#{x} * 1 = #{x*10}”
x = x + 1
end
Use the code above it’s working. The problem in your code is: “…”
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’ ?
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” [email protected] wrote on 02/21/2014
11:38:23
AM:
From: Regis d’Aubarede [email protected]
To: [email protected]
Date: 02/21/2014 11:38 AM
Subject: Re: How to do table of multiplication of number program in
Ruby?
Raja gopalan wrote in post #1137469:
hi Jaimin P.,
x = 1
while x <= 10
puts “#{x} * 1 = #{x} , #{x} * 2 = #{x2}, #{x} * 3 = #{x3}…
#{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.
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.
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.
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}=#{xn}”+"\t"
end
x += 1
end
Matthew K. wrote in post #1137468:
Jaimin P. 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.
Jaimin P. 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}=#{xn}”+"\t"
end
x += 1
end
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}=#{xn}”+"\t"
end
x += 1
end
I got desire output from your answer.
Thank you very much.
Jaimin P. 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}=#{ab}”+"\t"
end
end
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}=#{ab}”+"\t"
end
end
In above program, one code “puts “\n” if b == 1” is missing above this
code( print “#{a}#{b}=#{ab}”+"\t" ).
Thank you.
if i give the input as num 5 ishould get the output as
51=5
52=10
53=15
54=20
55=25
56=30
57=35
58=40
59=45
510=50
i need an urgent reply
(“01”…“99”).each { |s| a,b=s.chars.map(&:to_i); c=ab
print “%6s=%-2d” % ["%d%d"% [a,b],c] if c> 0
puts if b==9
}
Pranay reddy Sallaram wrote in post #1161867:
if i give the input as num 5 ishould get the output as
51=5
52=10
53=15
54=20
55=25
56=30
57=35
58=40
59=45
510=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}=#{xn}”+"\n"
end
end
end
mail1 = Ruby.new
mail1.function
i think it may help you