Member Avatar for sravan953

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?

Member Avatar for sravan953

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();
            }
        }
    }

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 .........................................................

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();
		}
		
	}
}
commented: What are you trying to do? -3

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

Use an array and a loop.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.