Results 1 to 17 of 17
  1. #1
    Anandt88 is offline Member
    Join Date
    Jun 2010
    Posts
    9
    Rep Power
    0

    Default Triangle using Numbers

    Hello Everyone,

    I want the code for which the o/p ll be

    1
    2 2
    3 3 3
    4 4 4 4
    and so on....

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,771
    Blog Entries
    7
    Rep Power
    21

    Default

    Java Code:
    System.out.println("                        1");
    System.out.println("                   2        2");
    System.out.println("              3        3       3");
    System.out.println("        4        4         4        4");
    System.out.println("and so on....");
    You're welcome.

    kind regards,

    Jos

  3. #3
    Anandt88 is offline Member
    Join Date
    Jun 2010
    Posts
    9
    Rep Power
    0

    Default

    Hello Jos,

    Thank You for the reply, but i wasn't expecting this....Anyways this is the code i hav written

    public class Triangle
    {
    public static void main(String[] args)
    {
    int n = Integer.parseInt(args[0]);
    for(int i=1;i<=n;i++)
    {
    for(int j=n;j>=i;j--)
    {
    System.out.print(" ");
    }
    for(int k=1;k<=i;k++)
    {
    System.out.print(i+" ");
    }
    System.out.println("");
    }

    }
    }
    but i wanna give more spaces...can u give me a hint

    Thanks n Regards

    Anand

  4. #4
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Lightbulb

    Woo
    What about this one :rolleyes:

    void gogo(int limit){

    for(int i=0; i<limit; i++)
    {
    for(int j=0; j<i+1; j++)
    {
    System.out.print(i+1);
    }
    System.out.println();
    }
    }
    If my answer helped you. Please click my "REP" button and add a comment
    Have a Good Java Coding :)

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,771
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Webuser View Post
    Woo
    What about this one :rolleyes:

    void gogo(int limit){

    for(int i=0; i<limit; i++)
    {
    for(int j=0; j<i+1; j++)
    {
    System.out.print(i+1);
    }
    System.out.println();
    }
    }
    Nah,

    a) shakey loop bounds
    b) no spaces between the numbers
    c) no code tags in your reply.

    So, no, no rep points for you.

    Jos

  6. #6
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Question

    Quote Originally Posted by JosAH View Post
    Java Code:
    System.out.println("                        1");
    System.out.println("                   2        2");
    System.out.println("              3        3       3");
    System.out.println("        4        4         4        4");
    System.out.println("and so on....");
    You're welcome.

    kind regards,

    Jos
    Not bad... but how you think to "and so on"? Manually only :P
    If my answer helped you. Please click my "REP" button and add a comment
    Have a Good Java Coding :)

  7. #7
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Talking

    Quote Originally Posted by JosAH View Post
    Nah,

    a) shakey loop bounds
    b) no spaces between the numbers
    c) no code tags in your reply.

    So, no, no rep points for you.

    Jos
    But I thought the question is

    Hello Everyone,

    I want the code for which the o/p ll be

    1
    2 2
    3 3 3
    4 4 4 4
    and so on....
    :rolleyes:
    If my answer helped you. Please click my "REP" button and add a comment
    Have a Good Java Coding :)

  8. #8
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    307
    Rep Power
    6

    Default

    Where do you want to put more spaces? What is it doing now and what do you want it to be doing?

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,771
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Webuser View Post
    Not bad... but how you think to "and so on"? Manually only :P
    Nope, that was what the OP wanted: an "and so on ... " last line. I supplied "the" code so I'm a hero, hurray!

    kind regards,

    Jos ;-)

  10. #10
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Talking

    Quote Originally Posted by JosAH View Post
    Nope, that was what the OP wanted: an "and so on ... " last line. I supplied "the" code so I'm a hero, hurray!

    kind regards,

    Jos ;-)
    And still ? :rolleyes:
    Last edited by Webuser; 06-04-2010 at 09:41 PM.

  11. #11
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default

    Hey everyone

    Looks like this thread is about having a laugh or two. ;) Seriously, this is a textbook problem. :p

    Have fun guys
    Tim
    Last edited by tim; 06-04-2010 at 10:51 PM.
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  12. #12
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Talking

    Quote Originally Posted by Webuser View Post

    void gogo(int limit){

    for(int i=0; i<limit; i++)
    {
    for(int j=0; j<i+1; j++)
    {
    System.out.print(i+1);
    }
    System.out.println();
    }
    }
    Thank you for "improving", pal
    but starting loop from 0 it's a classic manner owing to OP ;)
    The thread author is speechless of choosing I guess :D

  13. #13
    Anandt88 is offline Member
    Join Date
    Jun 2010
    Posts
    9
    Rep Power
    0

    Default

    Hope u guys r having fun....but pls execute my code n then gimme a suggestion..

  14. #14
    Anandt88 is offline Member
    Join Date
    Jun 2010
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by StormyWaters View Post
    Where do you want to put more spaces? What is it doing now and what do you want it to be doing?
    Hello,

    Now its displaying a Equilateral Triangle using no's, but i want to give a tab space b/w the no's...the o/p am expecting is not visible exactly, so pls execute the program n let me know...n if i give number above 9 the triangular structure ll be destroyed...


    Thanks n Regards
    Anand

  15. #15
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,771
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Anandt88 View Post
    Hello Jos,

    Thank You for the reply, but i wasn't expecting this....Anyways this is the code i hav written

    public class Triangle
    {
    public static void main(String[] args)
    {
    int n = Integer.parseInt(args[0]);
    for(int i=1;i<=n;i++)
    {
    for(int j=n;j>=i;j--)
    {
    System.out.print(" ");
    }
    for(int k=1;k<=i;k++)
    {
    System.out.print(i+" ");
    }
    System.out.println("");
    }

    }
    }
    but i wanna give more spaces...can u give me a hint

    Thanks n Regards

    Anand
    Sure, I ran it (with '4' as an argument) and this was its output:

    Java Code:
        1 
       2 2 
      3 3 3 
     4 4 4 4
    What do you want me to suggest? It looks fine to me.

    kind regards,

    Jos

  16. #16
    Anandt88 is offline Member
    Join Date
    Jun 2010
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Sure, I ran it (with '4' as an argument) and this was its output:

    Java Code:
        1 
       2 2 
      3 3 3 
     4 4 4 4
    What do you want me to suggest? It looks fine to me.

    kind regards,

    Jos
    Ya rite, but if i give a no above 9 the triangular format ll be destroyed, i want the o/p it in the same way for the limit greater than 9 also...

    Thanks n Regards
    Anand

  17. #17
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,771
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Anandt88 View Post
    Ya rite, but if i give a no above 9 the triangular format ll be destroyed, i want the o/p it in the same way for the limit greater than 9 also...
    Start to use the printf( ... ) method and take care that all the numbers take up the same amount of spaces ...

    kind regards,

    Jos

Similar Threads

  1. ASCII Triangle
    By physics in forum New To Java
    Replies: 2
    Last Post: 03-13-2010, 02:00 AM
  2. Triangle
    By jkswebsite in forum New To Java
    Replies: 8
    Last Post: 01-10-2009, 03:08 PM
  3. printing two smallest numbers from a series of numbers
    By trofyscarz in forum New To Java
    Replies: 2
    Last Post: 10-15-2008, 12:46 AM
  4. Is it a right triangle? (Code help)
    By TheApostle in forum New To Java
    Replies: 8
    Last Post: 10-07-2008, 09:39 PM
  5. Pascal Triangle help
    By Magic101 in forum New To Java
    Replies: 4
    Last Post: 05-01-2008, 08:51 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
  •