Results 1 to 19 of 19
Like Tree1Likes
  • 1 Post By sunde887

Thread: Pattern printing in Java

  1. #1
    arunjib is offline Member
    Join Date
    Mar 2011
    Location
    West Bengal
    Posts
    29
    Rep Power
    0

    Default Pattern printing in Java

    i want to get following out for a java program... how i should start my study?


    output of the program:

    How many stars you want to print in 1st row ? 7

    *******
    ******
    *****
    ****
    *****
    ******
    *******

  2. #2
    Spyderpig is offline Member
    Join Date
    Feb 2011
    Posts
    24
    Rep Power
    0

    Default

    Java Code:
    	
    
    		System.out.println("*******");
    		System.out.println(" *   * ");
    		System.out.println("  * *  ");
    		System.out.println("   *   ");
    		System.out.println("  * *  ");
    		System.out.println(" *   * ");
    		System.out.println("*******");
    prolly something like this

  3. #3
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

  4. #4
    arunjib is offline Member
    Join Date
    Mar 2011
    Location
    West Bengal
    Posts
    29
    Rep Power
    0

    Default

    Quote Originally Posted by Spyderpig View Post
    Java Code:
    	
    
    		System.out.println("*******");
    		System.out.println(" *   * ");
    		System.out.println("  * *  ");
    		System.out.println("   *   ");
    		System.out.println("  * *  ");
    		System.out.println(" *   * ");
    		System.out.println("*******");
    prolly something like this
    sorry! i need it using loops not using output stream like u explained

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    What have you tried?

  6. #6
    arunjib is offline Member
    Join Date
    Mar 2011
    Location
    West Bengal
    Posts
    29
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    What have you tried?
    nothing idea till now. please start a hint i want to study...

  7. #7
    überfuzz is offline Member
    Join Date
    Feb 2011
    Posts
    43
    Rep Power
    0

    Default

    Try something in the lines of:
    for() {}
    While() {}

  8. #8
    arunjib is offline Member
    Join Date
    Mar 2011
    Location
    West Bengal
    Posts
    29
    Rep Power
    0

    Default

    Quote Originally Posted by überfuzz View Post
    Try something in the lines of:
    for() {}
    While() {}
    its very short hint sir, more please

  9. #9
    überfuzz is offline Member
    Join Date
    Feb 2011
    Posts
    43
    Rep Power
    0

    Default

    No problem sir. Let's see the loop you've got so far. :)

  10. #10
    arunjib is offline Member
    Join Date
    Mar 2011
    Location
    West Bengal
    Posts
    29
    Rep Power
    0

    Default

    Quote Originally Posted by überfuzz View Post
    No problem sir. Let's see the loop you've got so far. :)
    Java Code:
    for(i=0;i<n;i++) //n is number of * to print in first line
    {
    System.out.print("*");
    for(i=0;i<n-2;i++)
    System.out.println();
    System.out.print("*");
    }
    }

  11. #11
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Try thinking about it by the numbers. Look for a pattern in how the stars change.

  12. #12
    überfuzz is offline Member
    Join Date
    Feb 2011
    Posts
    43
    Rep Power
    0

    Default

    Pseudo code, just coding from my head. You should be able to get something out of it.
    Java Code:
    //if user is going to decide size, use a scanner
    control size % 2 = 1 // I think, uneven anyway.
    loop i from 0 to size
       if(i == 0 && i == size)
          //print whole line
       else
          loop j from 0 to size
          if(j == i && j == size - i)
             print *
          else
             print ' '

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

    Default

    Why not use a bit of 101 mathematics? Your figure exists in a square of size n times n; think of two axes ranging from -n/2 to n/2. A star must be drawn at a position (x,y) iff:

    1) y == n/2 or y == -n/2
    2) y == x or y == -x

    A simple method can implement this:

    Java Code:
    boolean isStar(int x, int y, int n) {
       return Math.abs(y) == n/2 || Math.abs(x) == Math.abs(y);
    }
    All you have to do is loop over the coordinates and print stars if they have to be printed:

    Java Code:
    for (int y= -n/2; y <= n/2; y++) {
       for (int x= -n/2; x <= n/2; x++) 
          System.out.print(isStar(x, y, n)?'*':' ');
       System.out.println();
    }
    et voila ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  14. #14
    nasim_khan is offline Member
    Join Date
    Jul 2011
    Posts
    2
    Rep Power
    0

    Default Re: Pattern printing in Java

    import java.io.*;
    class netpat
    {
    public static void main(String arg[])throws IOException
    {
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    System.out.print("How many * in first row ? : ");
    int n=Integer.parseInt(br.readLine());
    int i,j;
    for(i=1;i<=n;i++)
    {
    for(j=1;j<=n;j++)
    {
    if(i==1 || i==n)
    System.out.print("*");
    else
    if(i==j || i==n-j+1)
    System.out.print("*");
    else
    System.out.print(" ");
    }
    System.out.println();
    }
    }
    }

    this should give you your desired output....

  15. #15
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Don't spoon feed code. When posting code, use code tags. Also, this thread is a few months old and I'd imagine to op is either long gone, or at least solved the problem already.
    Dark likes this.

  16. #16
    nasim_khan is offline Member
    Join Date
    Jul 2011
    Posts
    2
    Rep Power
    0

    Default RE

    Quote Originally Posted by sunde887 View Post
    Don't spoon feed code. When posting code, use code tags. Also, this thread is a few months old and I'd imagine to op is either long gone, or at least solved the problem already.

    Thnx for the advice....I am new to this forum...will keep that in mind next time onwards...

  17. #17
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Quote Originally Posted by arunjib View Post
    i need it using loops
    No probs.
    Java Code:
    for(int i = 0; i < 7; i++) {
        switch(i) {
        case 0:
        case 6:
            System.out.println("*******");
            break;
        case 1:
        case 5:
            System.out.println(" *   * ");
            break;
        case 2:
        case 4:
            System.out.println("  * *  ");
            break;
        case 3:
            System.out.println("   *   ");
            break;
        }
    }

  18. #18
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    i need it using loops
    No probs.
    Sheesh.
    Java Code:
    for (int i = 0; i < 1; i++) {
        System.out.println("*******");
        System.out.println(" *   * ");
        System.out.println("  * *  ");
        System.out.println("   *   ");
        System.out.println("  * *  ");
        System.out.println(" *   * ");
        System.out.println("*******");
    }
    db

  19. #19
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Let me guess Daryl, you also declare arrays with a length of 1. ;)

Similar Threads

  1. strategy pattern and bridge pattern
    By jomypgeorge in forum New To Java
    Replies: 2
    Last Post: 12-13-2010, 05:13 AM
  2. Need to print pattern in java
    By Gan in forum New To Java
    Replies: 1
    Last Post: 10-20-2010, 08:36 AM
  3. Class pattern to generate following pattern:-
    By vxs in forum New To Java
    Replies: 5
    Last Post: 07-14-2010, 11:15 PM

Tags for this Thread

Posting Permissions

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