954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How do I get this pattern?

Here's the pattern:

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


Here's the code I used to get only the first part(the up-side down triangle):

class hourglass
{
static void pattern()
    {
        for(int j=1;j<=4;j++)
            {
                for(int i=1;i<=j;i++)
                    {
                        System.out.print(" ");
                    }
                for(int i=7;i>j;i--)
                    {
                        System.out.print("*");
                    }
                System.out.println();
            }
        }
    }


It isn't working, can anybody help?

sravan953
Posting Whiz in Training
243 posts since May 2009
Reputation Points: 2
Solved Threads: 30
 

I am using another code to get only the top up-side down triangle:

class hourglass
{
static void pattern()
    {
        for(int j=1;j<=4;j++)
            {
                for(int i=1;i<=j;i++)
                    {
                        System.out.print(" ");
                    }
                for(int i=4;i>=1;i--)
                    {
                        for(int p=1;p<=i;p++)
                            {
                                System.out.print("*");
                            }
                        }
                 System.out.println();
            }
        }
    }
sravan953
Posting Whiz in Training
243 posts since May 2009
Reputation Points: 2
Solved Threads: 30
 

Hello,
you can get the pattern by using "if and else" in the loop. Since it is not a continuous pattern so doing this throw a loop is not simple task .........................................................

gautam610
Newbie Poster
13 posts since Jul 2008
Reputation Points: 5
Solved Threads: 3
 
public class A {
	public static void main(String[] main) {
		int length=7;
		
		for(int i=length; i>0; i-=2) {
			for(int j=length-i; j>=0; j--) System.out.print(" ");
			for(int k=i; k>0; k--) System.out.print("*"); 
			if(i!=1) System.out.println();
		}
		for(int i=-1; i<=length; i+=2) {
			for(int j=0; j<=length-i; j++) System.out.print(" ");
			for(int k=0; k<i; k++) System.out.print("*"); 
			System.out.println();
		}
		
	}
}
Lensva
Junior Poster in Training
76 posts since Mar 2008
Reputation Points: 4
Solved Threads: 3
 

this
this is
this is a
this is a test
how to print this??

shahrukhali5
Newbie Poster
1 post since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

Use an array and a loop.

Next time start a new thread if you have a question. Don't hijack someone else's.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You