Computer Science, asked by TreeshaBiswas, 5 months ago

print the pattern using JAVA PROGRAMMING​

Attachments:

Answers

Answered by anindyaadhikari13
5

Required Answer:-

Question:

  • Java program to display the given pattern.

Solution:

This is the required program.

public class Pattern {

 public static void main(String[] args) {

     int i,j,n=4, space=n-1;

     for(i=1;i<=n;i++,space--)  {

        for(j=1;j<=space;j++)

          System.out.print(" ");

        for(j=1;j<=i;j++)

          System.out.print(j);

        for(j=i-1;j>=1;j--)

          System.out.print(j);

        System.out.println();

     }

     for(i=n-1,space=1;i>=1;i--,space++)  {

       for(j=1;j<=space;j++)

          System.out.print(" ");

       for(j=1;j<=i;j++)

          System.out.print(j);

       for(j=i-1;j>=1;j--)

          System.out.print(j);

       System.out.println();

     }

 }

}

Explanation:

I have divided the pattern into two parts,

     1

   121

 12321

1234321

and,

 12321

    121

     1

For the first part, there are n - i spaces in each line where i is the row number.

The first pattern is again divided into two parts(ignoring spaces),

     1

   12

 123

1234

and,

1

21

321

For the first part, each row contains numbers from 1 to 'i' where 'i' is the row number.

After displaying the first part, second part is displayed. It is just the opposite of the first part.

After displaying the first part of the pattern, the second part of the given pattern is displayed. second part is just the reverse of the first part.

See the attachment for output ☑.

Attachments:
Similar questions