 Home >>Java Programs >Java Program to Print the following pattern

# Java Program to Print the following pattern

### Java program to print the following pattern

Pattern:
```12344321
123**321
12****21
1******1
```

In this example, we will print the above given pattern on console using a Java program.

Algorithm:
• Step 1: Start.
• Step 2: Set i=1,j=1,k=1,l=1,direction=1.
• Step 3: Set matrix.
• Step 4: Repeat Step 5 to Step 9 Until i is less than 10.
• Step 5: Set j=0.
• Step 6: Repeat Step 7 and Step 8 Until j is less than 10.
• Step 7: Set matrix[i][j]=0.
• Step 8: Set j=j+1.
• Step 9: Set i=i+1.
• Step 10: Set i=0.
• Step 11: Repeat Step 12 to Step 16 Until i is less than 10.
• Step 12: Set j=0.
• Step 13: Repeat Step 14 and Step 15 Until j is less than 10.
• Step 14: Print matrix[i][j].
• Step 15: Set j=j+1.
• Step 16: Set i=i+1.
• Step 17: Set i=1.
• Step 18: Set j=0.
• Step 19: Set k=0.
• Step 20: Repeat Step 21 to Step 26 Until i is less than 100.
• Step 21: Set matrix[j][k]=i.
• Step 22: If direction=1 If k+1<10 If matrix[j][k+1]==0 Set k=k+1 Else Set j=j+1 and Set direction =2 Else Set j= j+1 and Set direction =2.
• Step 23: IF direction=2 IF j+1<10 IF matrix[j+1][k]==0 Set j=j+1 Else Set k=k-1 and Set direction =3 Else Set k= -1 and Set direction =3.
• Step 24: IF direction=3 IF k-1>=0 IF matrix[j][k-1]==0 Set k=k-1 Else Set j=j-1 and Set direction =4 Else Set j= j-1 and Set direction =4.
• Step 25: IF direction=4 IF j-1>=0 IF matrix[j-1][k]==0 Set j=j-1 Else Set k=k+1 and Set direction =1 Else Set k= k+1 and Set direction =1.
• Step 26: Set i=i+1.
• Step 27: Set i=0.
• Step 28: Repeat Step 29 to Step 33 Until i is less than 10.
• Step 29: Set j=0.
• Step 30: Repeat Step 31 and 32 Until j is less than 10.
• Step 31: Print matrix[i][j].
• Step 32: Set j=j+1.
• Step 33: Set i=i+1.
• Step 34: End.
Program:
``````
public class Main
{
public static void main(String[] args) {
int lines=4;
int i,j,k,l;
int space=0;
for(i=0;i<lines;i++){// this loop is used to print lines
for(j=1;j<=space;j++){// this loop is used to print space in a line
System.out.print(" ");
}
for(j=1;j<=lines;j++){// this loop is used to print numbers in a line
if(j<=(lines-i))
System.out.print(j);
else
System.out.print("*");
}
j--;
while(j>0){// this loop is used to print numbers in a line
if(j>lines-i)
System.out.print("*");
else
System.out.print(j);
j--;
}
if((lines-i)>9)// this loop is used to increment space
space=space+1;
System.out.println("");
}
}
}
```
```
Output:
12344321
123**321
12****21
1******1