5 Replies - 2108 Views - Last Post: 18 March 2011 - 05:07 PM Rate Topic: -----

#1 MoonBeam080680  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

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

Reputation: 8343
  • View blog
  • Posts: 31,890
  • 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

Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

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

Reputation: 8343
  • View blog
  • Posts: 31,890
  • 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);
				
			}


Was This Post Helpful? 1
  • +
  • -

#4 MoonBeam080680  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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);
		
	}
      }   
   }   
}   


Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10786
  • View blog
  • Posts: 40,168
  • 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.
Was This Post Helpful? 1
  • +
  • -

#6 MoonBeam080680  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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! :bigsmile:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1