• Post Reply Bookmark Topic Watch Topic
  • New Topic

Triangular star program  RSS feed

 
sriram vaitheeswaran
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone..

Today I attended an interview.
I was asked a program to get a output like this..

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

Set of stars as triangle, without gaps in between stars..

Can anyone help me out in solving this ?
 
sriram vaitheeswaran
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry...

The out put should be like this...



But the twist is, the space in between stars must not be there...
But the output must be similar to this like a triangle.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nobody here will just give you the answer. We expect you to ShowSomeEffort.

Try to do it yourself. If it doesn't work, and you can't figure out why, post your code here (and please, UseCodeTags), tell us what it does, what it doesn't do, what you expect it to do, etc.
 
sriram vaitheeswaran
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The HTML code for output I needed...


<center>*<br/>**<br/>***<br/>****<br/>*****<br/></center>


The code which I tried....
This is in C++ functions...
So don mind.. I need a logic to get the above output..

 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so...work out the logic. Forget about java, C, HTML...

tell us in ENGLISH what you need to do.

I mean, one solution would be to print five lines:

print four spaces, then a star all on the same line
print three spaces, then two stars on the same line
..etc

now, that works, but it's not a very efficient way to code...especially if tomorrow, you need to print a triangle with 30 lines, or 3000 lines.

Can you try to generalize the above, but again, not using programming terms, but English?
 
sriram vaitheeswaran
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes..
I can generalize..

But its not aligned center..
Just type what you said in notepad to check..
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed, and then you would still have some spacing in the vertical direction.

No, there is a far more simple method to achieve what the interviewer wants. Here goes:
create a bufferedImage, size doesn't matter, just as I always tell my wife.
from the g2d grapics, measure the size of an asterisk in any font you like. Use getAscent() and so.
create a second bufferedImage, with exactly that size, transparent background of course, and draw that asteriks.
Next create a TexturePaint() with that bufferedImage.
And then let that interviewer draw any triangle at all in some JPanel
and when he's finished (or she), start a timer
and every time that timer fires, apply a rotation and a scaling, increasing as time goes by
then draw that triangle filled with that texturePaint
well, you could make it a little more challenging by using OpenGL. Wrap the whole thing
on a rotating sphere and turn lighting on.

Greetz,
Piet
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would think using a styled text component and the HTML provided above would be easier than painting on an image. But that is just me.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sriram vaitheeswaran, please BeForthrightWhenCrossPostingToOtherSites
http://www.java-forums.org/new-java/80850-triangular-star-program.html
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wait...what do you mean "aligned center"? Your original post made no mention of any kind of alignment, just "print a triangle of stars".

I don't see how your C code would center them (although admittedly my C is a little rusty).

Please tell us what your exact requirements really ARE if you want us to be able to help you.

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sriram vaitheeswaran wrote:But its not aligned center...

First, try to type out such star in Notepad without a program. I think you'll find it's impossible to do as you've described with a mono-spaced font and no spaces, so there's not really much point in writing a program. (Hint: there IS a way to do it; but not as you described)

My advice (and it's similar to the others): Find out exactly what it is you do need to do; because it sounds to me like you've misunderstood the instructions.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!