3 Replies - 4699 Views - Last Post: 16 October 2010 - 07:39 PM Rate Topic: -----

#1 Guest_keem*


Reputation:

java center pyramid

Posted 16 October 2010 - 01:22 AM

can guys help me? im stuck in making this a WHOLE pyramid. im usually getting only half of it. the right side is missing.
the pattern should be like this:
_____1
___1 2 1
_1 2 4 2 4 1
1 2 4 8 4 2 1

here's my code.
hope you know how to solve this thing. tnx! :)

class test{
        public static void main(String[]args){

  for(int row = 1; row <= 128; row=row*2){
          System.out.println();

                 for(int column = 1; column <= 128; column=column*2)
                         System.out.print("   ");

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

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


  }
 }
}



Is This A Good Question/Topic? 0

Replies To: java center pyramid

#2 Martyr2  Icon User is online

  • Programming Theoretician
  • member icon

Reputation: 4307
  • View blog
  • Posts: 12,085
  • Joined: 18-April 07

Re: java center pyramid

Posted 16 October 2010 - 03:49 PM

Here is a sample code to play with and tinker.

class test{
    public static void main(String[]args){
        final int ROWS = 10;

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

            // Spaces is the number of total rows - current row.
            for (int spc = ROWS - row; spc > 0; spc--) {
                System.out.print("  ");
            }
			
            // Left half of a given row
            int y = 1;
            for (int col = 1; col <= row; col++) {
                System.out.print(y);
                System.out.print(" ");
                y = y + y;
            }
			
			
            // Right half of a given row
            y = y/2;
            for (int col = row; col > 1; col--) {
                System.out.print(y/2);
                System.out.print(" ");
                y = y/2;
            }				
		
            // New row
            System.out.println();
        }
    }
}




See how that works and try to understand what is going on here. :)
Was This Post Helpful? 1
  • +
  • -

#3 jimdandy75  Icon User is offline

  • D.I.C Regular

Reputation: 37
  • View blog
  • Posts: 311
  • Joined: 30-June 08

Re: java center pyramid

Posted 16 October 2010 - 04:24 PM

Here's some more code to play with. This is a similar program.
http://www.dreaminco...ing-a-triangle/

This post has been edited by jimdandy75: 16 October 2010 - 04:24 PM

Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

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

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: java center pyramid

Posted 16 October 2010 - 07:39 PM

Looks like a basic Pascal triangle
Search the forum for Pascal
you'll have hundredth of examples some using recursion and some without
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1