# Java Pyramid numbers multiply by 2

Page 1 of 1

## 5 Replies - 2419 Views - Last Post: 18 March 2011 - 05:07 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=222575&amp;s=a606dfd5a0067f2849e30366fffc102b&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 MoonBeam080680

Reputation: 0
• Posts: 29
• Joined: 05-February 11

# Java Pyramid numbers multiply by 2

Posted 16 March 2011 - 07:24 PM

I have figured out how to get the pyramid to display, but I cannot figure out where to put the *2 to make it work. Any help would be greatly appreciated. Here is what I have so far.

import java.util.Scanner;

public class Pyramid {

public static void main (String [] args){

int numberOfLines;
numberOfLines = 8;

for (int row = 1; row <= numberOfLines; row++) {

for (int column = 1; column <= numberOfLines - row; column++)
System.out.print("   ");

for (int num = row; num >= 1; num--)
System.out.print((num >=10) ? " " + num  : "  " + num );

for (int num = 2; num <=row; num++)
System.out.print((num>= 10) ? " " + num  : "  " + num );

System.out.println();

}
}
}

The output should look like this:

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
(but in the pyramid shape)

Is This A Good Question/Topic? 0

## Replies To: Java Pyramid numbers multiply by 2

### #2 pbl

• There is nothing you can't do with a JTable

Reputation: 8362
• Posts: 31,955
• Joined: 06-March 08

## Re: Java Pyramid numbers multiply by 2

Posted 16 March 2011 - 07:46 PM

Sorry, now I see what you mean

This post has been edited by pbl: 16 March 2011 - 07:50 PM

### #3 pbl

• There is nothing you can't do with a JTable

Reputation: 8362
• Posts: 31,955
• Joined: 06-March 08

## Re: Java Pyramid numbers multiply by 2

Posted 16 March 2011 - 07:59 PM

Simplest way use a temporay variable k and * 2 at each iteration
/ 2 fro the right part

for (int row = 1; row <= numberOfLines; row++) {

for (int column = 1; column <= numberOfLines - row; column++)  {
System.out.print("   ");
}

int k = 1;
for (int num = row; num >= 1; num--)  {
System.out.print((k >=10) ? " " : "  ");
System.out.print(k);
k *= 2;
}
k /= 2;
for (int num = 2; num <=row; num++)  {
k /= 2;
System.out.print((k>= 10) ? " " : "  ");
System.out.print(k);

}

### #4 MoonBeam080680

Reputation: 0
• Posts: 29
• Joined: 05-February 11

## Re: Java Pyramid numbers multiply by 2

Posted 18 March 2011 - 04:54 PM

That helped a lot with getting the correct numbers, but it is no longer in a pyramid shape. I dont think I misplaced anything and I am getting no errors.

Here is what I have now.

public class Pyramid {

public static void main (String [] args){

int numberofLines;
numberofLines = 8;

for (int row = 1; row <= numberofLines; row++) {

for (int column = 1; column <= numberofLines - row; column++)  {
System.out.print("   ");
}

int k = 1;

for (int num = row; num >= 1; num--)  {
System.out.print((k >= 10) ? " " : "  ");
System.out.print(k);

k *= 2;
}
k /= 2;

for (int num = 2; num <=row; num++)  {
k /= 2;
System.out.print((k >= 10) ? " "  : "  ");

System.out.print(k);

}
}
}
}

### #5 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11342
• Posts: 42,760
• Joined: 27-December 08

## Re: Java Pyramid numbers multiply by 2

Posted 18 March 2011 - 05:00 PM

You need a System.out.println() at the end of your outer for loop so the next set of numbers will be printed on the next line.

### #6 MoonBeam080680

Reputation: 0
• Posts: 29
• Joined: 05-February 11

## Re: Java Pyramid numbers multiply by 2

Posted 18 March 2011 - 05:07 PM

Thank you both so much! It works great!