Page 1 of 2 12 LastLast
Results 1 to 20 of 21
Like Tree6Likes

Thread: Triangular star program

  1. #1
    vsriram92 is offline Member
    Join Date
    Jun 2012
    Posts
    7
    Rep Power
    0

    Default 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. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default 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?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    vsriram92 is offline Member
    Join Date
    Jun 2012
    Posts
    7
    Rep Power
    0

    Default 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. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,935
    Rep Power
    4

    Default 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
    rajeev and noobplus like this.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default Re: Triangular star program

    Quote Originally Posted by vsriram92 View Post
    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...
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    vsriram92 is offline Member
    Join Date
    Jun 2012
    Posts
    7
    Rep Power
    0

    Default 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. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,083
    Rep Power
    4

    Default 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.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    vsriram92 is offline Member
    Join Date
    Jun 2012
    Posts
    7
    Rep Power
    0

    Default Re: Triangular star program

    Quote Originally Posted by gimbal2 View Post
    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. #9
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default 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.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #10
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,083
    Rep Power
    4

    Default Re: Triangular star program

    Quote Originally Posted by vsriram92 View Post
    I need the similar output without spacing...
    Like I said - forget about code. Try create it using a text editor. Good luck!
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  11. #11
    vsriram92 is offline Member
    Join Date
    Jun 2012
    Posts
    7
    Rep Power
    0

    Default 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. #12
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Triangular star program

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  13. #13
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,935
    Rep Power
    4

    Default Re: Triangular star program

    Quote Originally Posted by vsriram92 View Post
    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
    gimbal2 likes this.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  14. #14
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,083
    Rep Power
    4

    Default Re: Triangular star program

    Quote Originally Posted by jim829 View Post
    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
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  15. #15
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,004
    Blog Entries
    7
    Rep Power
    20

    Default Re: Triangular star program

    Quote Originally Posted by jim829 View Post
    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)
    cenosillicaphobia: the fear for an empty beer glass

  16. #16
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,083
    Rep Power
    4

    Default 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.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  17. #17
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,004
    Blog Entries
    7
    Rep Power
    20

    Default Re: Triangular star program

    Quote Originally Posted by gimbal2 View Post
    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.
    cenosillicaphobia: the fear for an empty beer glass

  18. #18
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,083
    Rep Power
    4

    Default 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:
        +
        |
        *
       *.*
      *.*.*
     *.*.*.*
    *.*.*.*.*
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

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

    Default 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. #20
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,004
    Blog Entries
    7
    Rep Power
    20

    Default Re: Triangular star program

    Quote Originally Posted by chandrasekhar.melam View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

Page 1 of 2 12 LastLast

Similar Threads

  1. Java Applet Warning triangular icon
    By pink123 in forum Java Applets
    Replies: 1
    Last Post: 10-12-2011, 10:51 AM
  2. Star character in program arguments
    By CodesAway in forum Eclipse
    Replies: 2
    Last Post: 10-28-2009, 02:01 PM
  3. Need Help with A star
    By 256mxr in forum New To Java
    Replies: 3
    Last Post: 10-11-2009, 02:50 PM
  4. [SOLVED] Help Me with Printing Asterisk Triangular
    By AFstudent in forum New To Java
    Replies: 20
    Last Post: 11-12-2008, 03:59 PM

Posting Permissions

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