• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to print triangle like format

 
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i need to know how to print format's in java, i mean like triangle and all,
i.e) 1
101
10001
1000001
100000001

a
ab
abc
abcd
abcde

this type of formats, can anyone tell me is there any logic or trick to do this??
above two examples are simple, but many are complex in this, so is there any way???
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What exactly do you mean? It's very easy to print 1, 101, 10001, etc. or a, ab, abc, abcd etc.

 
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
    1
   101
  10001
 1000001
100000001


Do you mean like this?

Just use whitespace
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The printing isn't anything special. If you're looking for that sort of pattern, it's what you print that matters.

So for the first one, you need a loop counting n = 1, 3, 5, 7, ... Then, inside that, you need to construct a string starting and ending with 1, and containing (n - 2) zeros (with the first one described slightly differently, of course). You could use a nested loop to do that.
 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i dosen't mean, that simply print it using 'system.out.println()'
i mean to print these type of patterns using loop, generally we use two for loops for these type of pattern but if the pattern has spaces then we add one more loop,
but i always confuse in this type of situation that where to start which loop, and upto when loop will go, i mean how the initialization, condition and increment of loop, i know it's quietly depend on the pattern we are printing but is there any way or trick or mechanism to get/know the initialization, condition and increment of loop..
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One thing is.....always you need to think as row and column .....one loop for row another loop for column...that should be the starting trick...

Like for yuor case..
Row1 numbers of columns 1
Row2 numbers of columns 3
Row3 numbers of columns 5.
...
...
....
Rown numbers of column n+2


 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i agree, and consider i for rows and j for column, but what abut next step..
 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry if it looks a silly question, but how to decide the logic, any way/trick??
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Goutam Chowdhury wrote:For your case n=5....



Please don't do that. This site is NotACodeMill. Doing the OP's work for him does not help him learn, and as it clearly states on the topic list page, "We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers."
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Punit Jain wrote:sorry if it looks a silly question, but how to decide the logic, any way/trick??



No, there's no trick, and figuring out the logic has nothing to do with Java. It's a matter of seeing the pattern and then expressing it clearly and precisely in very simple steps in English or your native language or pseudocode or whatever mixture your most comfortable with. Only after you have expressed it in an informal, natural form should you try to convert that to Java.

For instance, here you might look at a few samples and realize, that line 1 has 1 digit, line 2 has 3, then 5, then 7, and so on. From that you can easily write an equation that expresses the number of digits for a give line. That's just basic algebra. You can also do the same with which digits are 1, which are 0, and how many leading spaces. Once you have that figured out and can express is precisely, then you convert it to Java.
 
Goutam Chowdhury
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:

Goutam Chowdhury wrote:For your case n=5....



Please don't do that. This site is NotACodeMill. Doing the OP's work for him does not help him learn, and as it clearly states on the topic list page, "We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers."



Sorry Jeff...I thought It will help him with explanation
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Goutam Chowdhury wrote:

Jeff Verdegan wrote:

Goutam Chowdhury wrote:For your case n=5....



Please don't do that. This site is NotACodeMill. Doing the OP's work for him does not help him learn, and as it clearly states on the topic list page, "We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers."



Sorry Jeff...I thought It will help him with explanation



The code for an example of a problem that has a similar structure to his but is not identical would help him. Just writing the code for him that does exactly what he's asking about does not. (I assume your code does that, but I didn't look closely at it or run it, so if it is in fact just a similar example, then no worries, that's fine.)

Not a big deal. Just try to remember in the future that the goal is to guide people toward finding their own answers.
 
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Punit Jain wrote:sorry if it looks a silly question, but how to decide the logic, any way/trick??



That is the fun bit, looking at the problem and having to work out the how.
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:. . . Just writing the code for him that does exactly what he's asking about does not [help]

You are quite right about that. Thank you for explaining it.

I have deleted the two posts with solutions in; it may be possible to restore them later (if I remember).
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic