I am building a program to make a pyramid as such.
1
1 2 1
1 2 4 2 1
Etc. For 8 rows.

public class PrintingNumbers {
	public static void main (String [] args) {
		
		int k = 1;
				
		for (int i = 1; i <= 8; i++) {
			
			for (int j = 1; j <= 8 - i; j++){
				System.out.printf("%5s", "");
			}
				for (k = 1; k < i*2;) {
					System.out.printf("%5d", k); 
					k=k*2;
				}
				for (k=k/2; k >= 2;){
					k=k/2;
					System.out.printf("%5d", k);
				}
		System.out.println();	
		}
	}
}

Currently this is my code. It works until the 4th iteration where the continuation statement for my loop becomes k = 8 and 8 !> 8. I need help trying to devise a better continuation statement for my loop so it stays with the format. Any help with be appreciated.

Recommended Answers

All 6 Replies

What have you tried? What happens when you change the hardcoded 8s to a variable and then set that variable to the number that you want?

Post the output that shows the problem and explain what is wrong with it.

What is the algorithm that describes what goes on each row.
What goes on row 1.
What goes on row 2.
etc

What is the relationship between what goes on a row and the row number?

1
                                  1    2    1
                             1    2    4    2    1
                        1    2    4    2    1
                   1    2    4    8    4    2    1
              1    2    4    8    4    2    1
         1    2    4    8    4    2    1
    1    2    4    8    4    2    1

This is the output when it should look like

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

except all the way down to 128

for (k = 1; k < i*2; )

i's maximum value will be 8 . Hence condition k < i * 2, means k can at the max be 16 . It will never go ahead of 16.

You can think of it like this : in terms of row no. and max possible value i.e. (We consider row 1 as row 0 )
row 0 , max possible 1
row 1 , max possible 2
row 2 , max possible 4
row 3 , max possible 8
row 4 , max possible 16
row 5 , max possible 32
row 6 , max possible 64
row 7 , max possible 128

Now , can you make out the pattern..????

So what you can do is, till you reach that max possible value, increment(here multiply) accordingly, after that , decrement(here divide) accordingly.

Also note the number of elements in each row. They follow a pattern too. Try guessing that on your own . If you don't get it we will help you. You can then use that value as condition in your loop. Enjoy coding :)

I used your tips DJsan, and fixed it with such an easy way! Took me like 4 hours to figure out how to put in one line of code and fix it! Lol Thanks :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.