programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# PowerOf2Triangle

Ranch Hand
Posts: 58
I want to make this triangle:
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

Thanks

Rancher
Posts: 2745
31

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.

Lindsey Brooks
Ranch Hand
Posts: 58
Here is my code so far:

Sheriff
Posts: 11629
187
Well, since the only statements you've written to print something out print either a 1, a space, or nothing, I assume this isn't doing what you need it do.

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.

Lindsey Brooks
Ranch Hand
Posts: 58
Never mind about the one on top.
Here's what I have so far:

Lindsey Brooks
Ranch Hand
Posts: 58
I got it.

Thanks for everyone's help.

Marshal
Posts: 57437
175

Lindsey Brooks wrote:. . .

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
System.out.println(myStringBuilder);
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.

Campbell Ritchie
Marshal
Posts: 57437
175

Lindsey Brooks wrote:I got it. . . .

You have a habit of claiming success just because you get the desired output, whilst posting suboptimal code. You would do better to post your final offering and allow us to improve it.

Bartender
Posts: 768
27

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);

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
Marshal
Posts: 57437
175

Ganesh Patekar wrote:. . , Is It fine to append "\n" each time a. . . .

No. You sh‍ould 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
Bartender
Posts: 768
27
As I have java version "1.8.0_101" so at this moment I used lineSeparator() which returns system-dependent line separator string introduced in 7 means I can run my program on any platform like M Windows (Uses "\r\n") and UNIX (Uses "\n)

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.

Campbell Ritchie
Marshal
Posts: 57437
175
• 1

Ganesh Patekar wrote:. . . . does that mean System.out.printf("%s%n","Java"); will work on both Windows and UNIX? . . . .

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
Bartender
Posts: 768
27

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:-

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

 I'm still in control here. LOOK at this tiny ad! The WEB SERVICES and JAX-RS Course https://coderanch.com/t/690789/WEB-SERVICES-JAX-RS