# Triangular star program

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• 08-14-2013, 08:45 PM
vsriram92
Triangular star program
Hello everyone..

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

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 ?
• 08-14-2013, 08:58 PM
KevinWorkman
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?
• 08-14-2013, 09:04 PM
vsriram92
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.
• 08-14-2013, 09:08 PM
jim829
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
• 08-14-2013, 09:11 PM
KevinWorkman
Re: Triangular star program
Quote:

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...
• 08-14-2013, 09:15 PM
vsriram92
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..
• 08-14-2013, 09:39 PM
gimbal2
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.

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.
• 08-14-2013, 09:45 PM
vsriram92
Re: Triangular star program
Quote:

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.

Code:

```    *   ***   *****  ******* *********```

Hey.. Its an interview question... How come to change the question..!!!

I need the similar output without spacing...
• 08-14-2013, 09:46 PM
KevinWorkman
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.
• 08-14-2013, 09:51 PM
gimbal2
Re: Triangular star program
Quote:

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!
• 08-14-2013, 09:58 PM
vsriram92
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..
• 08-14-2013, 11:04 PM
DarrylBurke
Re: Triangular star program
• 08-15-2013, 01:24 AM
jim829
Re: Triangular star program
Quote:

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
• 08-15-2013, 09:58 AM
gimbal2
Re: Triangular star program
Quote:

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
• 08-15-2013, 10:11 AM
JosAH
Re: Triangular star program
Quote:

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)
• 08-15-2013, 10:16 AM
gimbal2
Re: Triangular star program
Doh! I just realized.

Code:

```* ** *** **** *****```
that is ALSO a triangle... Stupid trivia question, what are you applying for? A job or being professor Layton's apprentice?
• 08-15-2013, 11:07 AM
JosAH
Re: Triangular star program
Quote:

Originally Posted by gimbal2
Doh! I just realized.

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:

Quote:

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:

Code:

```    *   *.*   *.*.*  *.*.*.* *.*.*.*.*```
• 08-15-2013, 11:12 AM
gimbal2
Re: Triangular star program
indeed, too bad :) I guess yours is the best, its a nice ascii art version of a christmas tree too.

Code:

```    +     |     *   *.*   *.*.*  *.*.*.* *.*.*.*.*```
• 08-15-2013, 09:28 PM
chandrasekhar.melam
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
• 08-16-2013, 04:32 PM
JosAH
Re: Triangular star program
Quote:

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
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last