Pyramid Pattern

Dear Listers,

I am in the process of wrting a java code that prints the following pattern

123454321
1234*4321
123***321
12*****21
1*******1

This is the code that I have written as

public class Pyramid

{

    public static void main(String[] args)

    {

        int j;  

        for (int i=1; i<5; i++)
        {
            for(j=1; j<5; j++)
            {
                System.out.print(j);

            }

            for(int k=j; k>0; k--)

            {

                System.out.print(k);                
            }
                System.out.println();
        }

    }

}

My out put is
123454321
123454321
123454321
123454321

Reuqest some hint in acheiving the desired output.

Thanks
Vilas

Recommended Answers

All 4 Replies

Look at the pattern - all the rows are the same except
on the 2nd row the 5 is replaced by a *
on the next row the 4s and 5 are replace by an *
on the next row the 3,4,5 are replaced... etc

You can use an if test to decide for each position whether to print the number or an *

Well, you could take advantage of the way the numbers are patterned. Notice how your triangle fits perfectly if you replace everything less than or equal to N with asterisks, where N starts at 6:

int j;
int x = 6;

for (int i = 1; i < 6; i++, x--)
{
    for(j = 1; j < 5; j++)
    {
        if (j >= x)
        {
            System.out.print("*");
        }
        else
        {
            System.out.print(j);
        }
    }

    for(int k = j; k > 0; k--)
    {
        if (k >= x)
        {
            System.out.print("*");
        }
        else
        {
            System.out.print(k);
        }           
    }

    System.out.println();
}

Obviously this won't work without the incrementing and decrementing number pattern.

commented: Awesome!!! +0

Further to the above problem and logic I have executed the following on characters to solve the following pattern with alphabets.
File of the output as attached
ABCDEFGFEDCBA
ABCDEF*FEDCBA
ABCDE***EDCBA
ABCD*****DCBA
ABC*******CBA
AB*********BA
A***********A
attached is the code inline

public class Pattern
{

    public void alphaPyramid()
    {

            int j, x=72;

            for(int i=65; i<=72;i++, x--)
            {

            for(j=65; j<=70; j++)
            {

                if(j>=x)
                {
                    System.out.print(" ");
                }

                else
                {

                    System.out.print((char)j);
                }

            }


            for(int k=j; k>64; k--)
            {
                    if(k>=x)
                    {
                        System.out.print(" ");
                    }
                    else
                    {       
                        System.out.print((char)k);
                    }
            }

                System.out.println();

            }



    }


    public static void main(String[] args)

        {
            Pattern pyra = new Pattern();
            pyra.alphaPyramid();




        }

}

This is a good example and I will make a ref of this , is a good solve

Thanks all for helping me

Vilas

Your code would be more readable if you used char values: 'A' etc instead of the numbers: 65, 70, 72 etc

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.