File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to print triangle like format

 
Punit Jain
Ranch Hand
Posts: 1012
2
  • 0
  • Mark post as helpful
  • send pies
  • 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???
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 1
  • Mark post as helpful
  • send pies
  • 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.

 
Unnar Björnsson
Ranch Hand
Posts: 164
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
    1
   101
  10001
 1000001
100000001


Do you mean like this?

Just use whitespace
 
Matthew Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • 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: 1012
2
  • 0
  • Mark post as helpful
  • send pies
  • 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..
 
Goutam Chowdhury
Ranch Hand
Posts: 44
  • 0
  • Mark post as helpful
  • send pies
  • 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: 1012
2
  • 0
  • Mark post as helpful
  • send pies
  • 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: 1012
2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry if it looks a silly question, but how to decide the logic, any way/trick??
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 1
  • Mark post as helpful
  • send pies
  • 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.
 
Wendy Gibbons
Bartender
Posts: 1107
Eclipse IDE Oracle VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47258
52
  • 0
  • Mark post as helpful
  • send pies
  • 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).
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic