Home >>Java Programs >Java program to print the given pattern

# Java program to print the given pattern

### Java program to print the given pattern

Pattern

12344321
123**321
12****21
1******1

In this program, we will try to print the above given pattern on the console in java.

Algorithm:
• Step 1: Start.
• Step 2: Set lines=4.
• Step 3: Define i, j.
• Step 4: Set space=0.
• Step 5: Set i=0.
• Step 6: Repeat step 7 to step 20 until i<lines <li=""></lines<>.
• Step 7: Set j=1.
• Step 8: Repeat step 9 until j <= space.
• Step 9: Print " " and Set J=J+1.
• Step 10: Set j=1.
• Step 11: Repeat step 12 and step 13 until j<=lines.
• Step 12: If j <=(lines-i). Print j Else Print *.
• Step 13: Set j=j+1.
• Step 14: Decrement j by 1.
• Step 15: Repeat step 16 and step 17 until j>0.
• Step 16: If j>( lines-i ) then Print * Else Print j.
• Step 17: Set j=j-1.
• Step 18: If (lines-i) >9 increment space by 1.
• Step 19: Print a new line.
• Step 20: Set i=i+1.
• Step 21: End.
Program:
``````
public class Main
{
public static void main(String[] args)
{
int lines=4;
int i,j;
int space=0;
for(i=0;i<lines;i++)
{
for(j=1;j<=space;j++)
{
System.out.print(" ");
}
for(j=1;j<=lines;j++)
{
if(j<=(lines-i))
System.out.print(j);
else
System.out.print("*");
}
j--;
while(j>0)
{
if(j>lines-i)
System.out.print("*");
else
System.out.print(j);
j--;
}
if((lines-i)>9)
space=space+1;
System.out.println("");
}
}
}
```
```
Output:
12344321
123**321
12****21
1******1