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
I don't know how to do this in java please help.
how to do this
Take a piece of paper and write down the relationship between the numbers on each row and their relationship with the row they are on.
Look at that set of data and come up with some rules for generating those numbers.
Also find a rule for the number of leading spaces vs the row.
If you have problems making the rules, post what relationships you have found and the rules and ask some questions about the problems you are having.
You really need to spend some time analyzing the problem first and looking for similarities and differences between one line and the ones the precede and succeed it. Look for things that are repetitive. These indicate loops. Things that change consistently from one case to the next, those are increments or decrements or more generally, deltas. Start and end values would indicate your loop boundaries.
As I wrote in response to another of your questions, this is about developing a logical and systematic way of solving a problem that can be generalized to handle multiple cases.
Not happy with that. It would be far better to use the %d tag and use printf. It would be better still to use a StringBuilder and append the individual Strings with or without %d. Then you can collect the entire pyramid as a single String and print it with
Lindsey Brooks wrote:. . .
which may give faster performance than multiple tiny print calls.
Don't post commented‑out code. Only post code you are using. Don't comment out code with /* tags */. Always use // at the beginning of each line. That is because your code will fail to compile if you try to nest comments inside each other.
Yes I used StringBuilder for that, Is It fine to append "\n" each time at the end of for statement ( Top level for statement ) for printing output on next line? Problem is, If I don't append "\n" then when I print Stringbuilder after appending all, It prints all numbers on a single line.
Campbell Ritchie wrote:It would be better still to use a StringBuilder and append the individual Strings with or without %d. Then you can collect the entire pyramid as a single String and print it with System.out.println(myStringBuilder);
No. You should only use \n if somebody has told you they want the LF character. Otherwise append this (or similar). That maintains the correct form for your operating system.
Ganesh Patekar wrote:. . , Is It fine to append "\n" each time a. . . .
But what If I want to run my program on Java 6? I read 'n' is the platform-specific line separator Here, does that mean System.out.printf("%s%n","Java"); will work on both Windows and UNIX? I would have tried It practically but don't have any other OS except Windows.
Yes, it will give the correct line separator for the OS. Before that method was introduced, you had to say something like:-It is quite easy to run a computer as a dual‑boot machine running both Windows and Linux. You simply download a Linux distro onto a CD and install it. You can even run Linux as a trial application from the CD.
Ganesh Patekar wrote:. . . . does that mean System.out.printf("%s%n","Java"); will work on both Windows and UNIX? . . . .
Oh I see, day by day Java is making programmer's work easy. Yes I'll download a Linux distro so many threads I found on this forum will read It, If still has problem will ask you. Thank you
Campbell Ritchie wrote:Yes, it will give the correct line separator for the OS. Before that method was introduced, you had to say something like:-