Two Laptop Bag*
The moose likes Beginning Java and the fly likes how to print triangle like format Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to print triangle like format" Watch "how to print triangle like format" New topic
Author

how to print triangle like format

Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 980
    
    2
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

Joined: Aug 16, 2005
Posts: 14074
    
  16

What exactly do you mean? It's very easy to print 1, 101, 10001, etc. or a, ab, abc, abcd etc.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Unnar Björnsson
Ranch Hand

Joined: Apr 30, 2005
Posts: 164
    1
   101
  10001
 1000001
100000001


Do you mean like this?

Just use whitespace
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4344
    
    8

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

Joined: Aug 20, 2011
Posts: 980
    
    2
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

Joined: Jan 20, 2009
Posts: 44
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



Thanks And Regards
Goutam Chowdhury (Scjp1.4 86%,SCWCD5 94%)
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 980
    
    2
i agree, and consider i for rows and j for column, but what abut next step..
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 980
    
    2
sorry if it looks a silly question, but how to decide the logic, any way/trick??
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Jan 20, 2009
Posts: 44
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Oct 21, 2008
Posts: 1107

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

Joined: Oct 13, 2005
Posts: 38100
    
  22
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
 
subject: how to print triangle like format
 
Similar Threads
For Loop Problem
Formatting doubles in a toString return
Printing a triangle
super applied twice.
I need help - basic stuff (I think)