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

# Java program to print the following pattern

### Java program to print the following pattern

In this example, we will see a Java program in which we will print the above given pattern.

Algorithm:
• Step 1:Start.
• Step 2:Set lines=8
• Step 3:Define i and j.
• Step 4:Set i=1.Repeat Step 5 to 14 Until i<lines< li=""></lines<>
• Step 5:Set j=1.
• Step 6:Repeat Step 7 and 8 Until j <=(lines/2).
• Step 7:IF j is equals to i Print j
else if i is greater than 4 and j equals (lines-i) Print j
else Print " ".
• Step 8:j = j + 1.
• Step 9:j = j – 2.
• Step 10:Repeat Step 11 and 12 Until j > 0.
• Step 11:: if j is equals to i Print j
else if i is greater than 4 and j equals (lines-i) then Print j
else Print " ".
• Step 12:j = j – 1.
• Step 13:Print a new line.
• Step 14:i = i + 1.
• Step 15:End.
Program:
``````
public class Main
{
public static void main(String []args)
{
int lines=8;
int i,j;
for(i=1;i<lines;i++){// this loop is used to print the lines
for(j=1;j<=lines/2;j++){// this loop is used to print numbers
if(i==j)
{
System.out.print(j);
}
else if(i>4 && j==lines-i)
{
System.out.print(j);
}
else
{
System.out.print(" ");
}
}
j=j-2;
while(j>0)
{
//this loop is used to print numbers
if(i==j)
{
System.out.print(j);
}
else if(i>4 && j==lines-i)
{
System.out.print(j);
}
else
{
System.out.print(" ");
}
j--;
}
System.out.println("");
}
}
}
```
```
Output: