ruby - Pascal triangle using nested for loop -
i need primed shape. need output like:
1 0 1 0 1 0 1 0 1 0
i tried print left side of triangle, don't know print full triangle. code printing left side of triangle:
for in 1..5 j in 1..i if (i%2 == 0); k = (j%2 == 0) ? 1:0; else; k = (j%2 ==0) ? 0:1; end print k," " end puts end
i output this:
1 0 1 1 0 1 0 1 0 1 1 0 1 0 1
just add print " "*(5-i)
, this:
for in 1..5 print " "*(5-i) j in 1..i if (i%2 == 0); k = (j%2 == 0) ? 1:0; else; k = (j%2 ==0) ? 0:1; end print k," " end puts end
Comments
Post a Comment