1
1 2 1
1 2 4 2 1
1 2 4 8 4 2 1
1 2 4 8 16 8 4 2 1
1 2 4 8 16 32 16 8 4 2 1
1 2 4 8 16 32 64 32 16 8 4 2 1
1 2 4 8 16 32 64 128 64 32 16 8 4 2 1
This is what I have so far and it outputs the right half of the pyramid.
public class fourpointnineteen {
public static void main(String[] args) {
int c = 0;
for(int r = 0
; r <= 7; r++) {
for(c = 0; c <= r ; c++) {
if (c == 0) {
if (Math.pow(2,r) < 10)
System.out.print(" " + (int)Math.pow(2, r));
else if (Math.pow(2, r)< 100)
System.out.print(" " + (int)Math.pow(2, r));
else
System.out.print(" " + (int)Math.pow(2, r));
}
else {
if ( Math.pow(2, (r-c)) < 10)
System.out.print(" " + (int)Math.pow(2, (r-c)));
else if ( Math.pow(2, (r-c)) < 100)
System.out.print(" " + (int)Math.pow(2, (r-c)));
else
System.out.print(" " + (int)Math.pow(2, (r-c)));
}
}
System.out.println(" ");
}
}
}
Outputs the following:
[code] 1 2 1 4 2 1 8 4 2 1 16 8 4 2 1 32 16 8 4 2 1 64 32 16 8 4 2 1 128 64 32 16 8 4 2 1
My problem is that I'm not sure how to replicate this on the other side mainly due to the spacing. Do I have to count up the amount of spaces that need to be in front of each row?
Thanks in advance guys!

New Topic/Question
Reply



MultiQuote



|