1. Member
Join Date
Jun 2012
Posts
7
Rep Power
0

## Triangular star program

Hello everyone..

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

Java Code:
```     *
* *
* * *
* * * *
* * * * *```
Set of stars as triangle. The hard part is that, there should not be any space in between stars. But it should be similar to the output as shown above.

Can anyone help me out in solving this ?

2. ## Re: Triangular star program

What have you tried? Where are you stuck?

I recommend counting the number of spaces and stars in each line. Do you notice any patterns?

3. Member
Join Date
Jun 2012
Posts
7
Rep Power
0

## Re: Triangular star program

I tried this in "C++"

int main()
{

int c=1;
for(int i=1;i<=5;i++)
{
for(int j=i;j<5;j++)
{
cout<<" ";
}

for(int k=1;k<=c;k++)
{
if(k%2==0)
cout<<" ";
else
cout<<"*";
}
cout<<"\n";
c+=2;
}

}

I just need the logic...

The above code is for the program with space in between stars...
I need a code which don have space in between stars, but aligned in center.

4. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,840
Rep Power
10

## Re: Triangular star program

So all you were asked was to write a program to produce the output? They why all the loops? And do employers really ask this nonsense of software developers?

regards,
Jim

5. ## Re: Triangular star program

Originally Posted by vsriram92
I just need the logic...

The above code is for the program with space in between stars...
I need a code which don have space in between stars, but aligned in center.
If you can get it with spaces, why can't you use the same approach to figure out how to do it without spaces? What exactly are you confused about?

Is this an online interview? If so, you probably shouldn't be asking us to do it for you...

6. Member
Join Date
Jun 2012
Posts
7
Rep Power
0

## Re: Triangular star program

Ya... I cant get it out..
Seriously...

The reason is... If we remove spaces, It wont be center aligned..

You can check it using online compiler..

This question is not for an online test...
I failed in this question today at an interview..

So tot of getting the right answer..

7. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
9

## Re: Triangular star program

Forget about code for now. Try to draw such a triangle in a text editor by hand first.

I can't do it, its impossible without spacing. You'd have to draw a very different triangle.

Java Code:
```    *
***
*****
*******
*********```
And btw: don't feel too insecure about not being able to answer this. The world is also full of useless job interviewers/employers. The next interview you have may just focus more on what matters - you being able to do your job and not being able to answer stupid trivia questions.
Last edited by gimbal2; 08-14-2013 at 08:47 PM.

8. Member
Join Date
Jun 2012
Posts
7
Rep Power
0

## Re: Triangular star program

Originally Posted by gimbal2
Forget about code for now. Try to draw such a triangle in a text editor by hand first.

I can't do it, its impossible without spacing. You'd have to draw a very different triangle.

Java Code:
```    *
***
*****
*******
*********```
Hey.. Its an interview question... How come to change the question..!!!

I need the similar output without spacing...

9. ## Re: Triangular star program

I think the OP is confused about what the question is actually asking. I think the question is asking for exactly the triangle that gimbal2 just posted, which should be trivial if the OP can create the triangle-with-spaces in the first post.

10. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
9

## Re: Triangular star program

Originally Posted by vsriram92
I need the similar output without spacing...
Like I said - forget about code. Try create it using a text editor. Good luck!

11. Member
Join Date
Jun 2012
Posts
7
Rep Power
0

## Re: Triangular star program

Note that,
The output of gimbal2 has stars in the order of 1,3,5,7
I need it in the order of 1,2,3,4

@gimbal2 - We cant even get it in text editor too..

12. ## Re: Triangular star program

13. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,840
Rep Power
10

## Re: Triangular star program

Originally Posted by vsriram92
I need it in the order of 1,2,3,4
It's not possible unless you alter the font size or do it in a graphics context (which then brings up what constitutes a space). As far as I know you cannot modify the character spacing that way. Either the questioner didn't state the question properly or you didn't understand the question.

Once again, I find the question moronic. Next time, read up on the JLS and learn some esoteric stuff. Then ask the interviewer what they think of it. See how much they know. An interview works both ways.

Regards,
Jim

14. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
9

## Re: Triangular star program

Originally Posted by jim829
An interview works both ways.
Exactly that, although that starts to go into effect more and more as you gain experience. When I was a junior I jumped through every hoop to get a job, good or bad. Now 15+ years later, I'm making THEM sweat in an interview :p
Last edited by gimbal2; 08-15-2013 at 09:14 AM. Reason: typo

15. ## Re: Triangular star program

Originally Posted by jim829
It's not possible unless you alter the font size or do it in a graphics context
Or allow a 1x1 triangle ...

kind regards,

Jos (<--- lazy bum)

16. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
9

## Re: Triangular star program

Doh! I just realized.

Java Code:
```*
**
***
****
*****```
that is ALSO a triangle... Stupid trivia question, what are you applying for? A job or being professor Layton's apprentice?
Last edited by gimbal2; 08-15-2013 at 09:19 AM.

17. ## Re: Triangular star program

Originally Posted by gimbal2
Doh! I just realized.

Java Code:
```*
**
***
****
*****```
that is ALSO a triangle... Stupid trivia question, what are you applying for? A job or being professor Layton's apprentice?
Close but no cigar; the OP wrote in reply #3:

I need a code which don have space in between stars, but aligned in center.
kind regards,

Jos

ps. this triangle doesn't have any spaces inside of it:

Java Code:
```    *
*.*
*.*.*
*.*.*.*
*.*.*.*.*```
Last edited by JosAH; 08-15-2013 at 10:10 AM.

18. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
9

## Re: Triangular star program

indeed, too bad :) I guess yours is the best, its a nice ascii art version of a christmas tree too.

Java Code:
```    +
|
*
*.*
*.*.*
*.*.*.*
*.*.*.*.*```

19. Member
Join Date
Sep 2011
Location
India
Posts
67
Rep Power
0

## Re: Triangular star program

i think it's possible what the VSriram92 asks. The thing is the interviewer asks print triangle without using spaces. Yes we can print it in c-language. We have gotoxy() function. So the problem solved

20. ## Re: Triangular star program

Originally Posted by chandrasekhar.melam
So the problem solved
Only for a limited number of implementations of the C language (notably MS Windows implementations); the gotoxy( ... ) function isn't a member of C's Standard Library.

kind regards,

Jos

Page 1 of 2 12 Last

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•