Results 1 to 5 of 5
Like Tree2Likes
  • 2 Post By JosAH

Thread: print triangle shape

  1. #1
    Join Date
    Jun 2014
    Posts
    1
    Rep Power
    0

    Lightbulb print triangle shape

    hello my name is deshan . I m new to java & this forum... i want to print triangle shape using number like
    this
    1
    12
    123
    1234
    12345
    123456
    this is my code


    class shape{
    public static void main(String [] args){
    for(int x =0 ; x<=6;x++){
    for(int y =0 ; x > y ; y++){
    System.out.print(x);
    }
    System.out.print("\n");
    }
    }
    }

    but my output is
    1
    22
    333
    4444
    55555
    666666

    so can anyone help me to fix my code........thanks you

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

    Default Re: print triangle shape

    Start from the 'inside out'; first you want to print a single line of digits 1,2,3,4, ... this is how it's done:

    Java Code:
    for (int digit= 1; digit <= n; digit++)
       System.out.print(digit);
    System.out.println();
    This code snippet prints a single line with digits up to (and including) 'n'. But what is 'n'? Here it is:

    Java Code:
    for (int n= 1; n <= 6; n++)
       // code above goes here ...
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Abbhirami is offline Member
    Join Date
    Jun 2014
    Posts
    5
    Rep Power
    0

    Default Re: print triangle shape

    -This the exact code for the pattern:
    1
    12
    123
    1234
    12345
    123456

    package shape ;
    public class shape1 {
    public static void main(String [] args)
    {
    for(int x =1 ; x<=6;x++)
    {
    for(int y =1 ;y<=x ; y++)
    {
    System.out.print(y);
    }
    System.out.print("\n");
    }
    }

    }
    Regds,

    Abbhirami...

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

    Default Re: print triangle shape

    @Abbhirami: now you have spoonfed the OP; do you think the OP learns something from it or that s/he just copies and pastes the code and gets a nice grade (undeserved) for it? Don't do it anymore.

    Jos
    kneitzel and HeavyTech like this.
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Abbhirami is offline Member
    Join Date
    Jun 2014
    Posts
    5
    Rep Power
    0

    Default Re: print triangle shape

    @JosAH: Will not be doing that anymore.I thought that s/he would get an idea,from that.

Similar Threads

  1. Replies: 3
    Last Post: 09-16-2013, 08:51 PM
  2. Replies: 6
    Last Post: 01-07-2013, 02:48 PM
  3. Javax Print Attribute for Selection Print Range
    By rsawatzky in forum AWT / Swing
    Replies: 0
    Last Post: 04-26-2012, 12:14 AM
  4. Replies: 1
    Last Post: 04-02-2010, 12:48 PM
  5. Print the text file and print preview them
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-22-2008, 11:04 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
  •