File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes creating the triangle Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "creating the triangle" Watch "creating the triangle" New topic
Author

creating the triangle

Thilina Gunawardane
Greenhorn

Joined: Jul 23, 2010
Posts: 6
hey im thilina here....
im very new to here........

i can do the right angle triangle... in ths way
*****
****
***
**
*

bt i hve sme problem wth creating

*******
*****
***
*
ths kind of triangle.....

i need hlp for ths......its nt difficult 1 as i knw....bt i don't get da logic of tht.....
anyway... if any1 can help me wth ..im vry gratefull for tht.........thnx...


thnx alot.....
Regards,
Thilina







Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

Welcome Thilina

Try it using for loop.


Life is easy because we write the source code.....
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4531
    
    5

http://faq.javaranch.com/java/UseRealWords


luck, db
There are no new questions, but there may be new answers.
Thilina Gunawardane
Greenhorn

Joined: Jul 23, 2010
Posts: 6
thnx buddy....i did lyk tht b4 and it worked...bt da triangle i want cme up is the 1 i've given below

i want da triangle of the asterics.....

*****
a***a
aa*aa


sorry for da trouble earlier.......
da code i used is given below....
if any can hlp me wth tht... im vry grtfull thnx..




Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
here, fixed it so you can read it easily.
no charge.

subodh k kumar
Greenhorn

Joined: Aug 19, 2009
Posts: 16
This code will print the output as.

*******
*****
***
*



SCJP1.4 , SCWCD 5
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38519
    
  23
subodh k kumar, please note what is says on the "Beginning Java" contents 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.
It does not favours to anybody simply to hand out an answer; in fact if they simply copy your code and don't understand it, you can hinder their learning.

Don't be annoyed with me, but I have pulled rank and deleted the solution you posted.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38519
    
  23
Thilina Gunawardane, please read this.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38519
    
  23
You are going about your exercise the wrong way. You ought to write down very carefully and very simply what you need to do, with pencil and paper. If you use words with more than one syllable, then it doesn't count as "very simply".
Thilina Gunawardane
Greenhorn

Joined: Jul 23, 2010
Posts: 6

I'm really sorry about that.... I was not aware of the rules(Writing) at that time.....really sorry about it....
So anybody who can help to build this triangle

*****
a***a
aa*aa

i need the triangle with asterics...as i have shown above.......

Appreciate your'll effort....
thank you...
Regards,
Thilina.
akhter wahab
Ranch Hand

Joined: Mar 02, 2009
Posts: 151

Thilina Gunawardane wrote:

*****
a***a
aa*aa

i need the triangle with asterics...as i have shown above.......

Appreciate your'll effort....
thank you...
Regards,
Thilina.


where is your code that you have tried till now for doing this??


Start Earning Online||Start Earning Using Java
Naman Patidar
Greenhorn

Joined: Oct 03, 2008
Posts: 15
Try to break the problem in parts. In this case try to think the problem as 2 different triangles, Here is a sample solution :
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11257
    
  16

I'd approach it as two parts:

1) printing the correct number of '*' characters on each line
2) spacing them correctly.

You can do either part first. once you have that working, add the second. I'd try and find some relationship between the number of things (spaces or *'s) and the line number (i.e. loop counter) I'm currently on.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38519
    
  23
I earlier wrote: . . . please note what is says on the "Beginning Java" contents 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.
It does not favours to anybody simply to hand out an answer . . .
Naman Patidar, did you not see that post? I have taken your solution out


And I agree with Fred that such an exercise is much easier if broken into smaller parts.
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

What is wrong with the triangle solution you originally posted? Sure it prints out like this:

*
***
*****
*******

instead of this:

*******
*****
***
*

But that is easily fixable. Both Fred and Campbell are right about breaking this problem down into smaller parts, when I solve problems like this I like to think in Vertical and Horizontal.

Hunter


"If the facts don't fit the theory, get new facts" --Albert Einstein
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38519
    
  23
Does that include lateral thinking?
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

No. It's still a step-by-step process. I just think it is easier(in this case) to think of how many stars you need to print horizontally then how many rows to print vertically.

Hunter
 
 
subject: creating the triangle