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[10][10].
  • 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

Java Programs Check Palindrome Number in Java Factorial Program using loop in java Factorial Program using recursion in java Fibonacci Series Program in Java using recursion Fibonacci series without using recursion in Java Find an Armstrong Number in Java Prime Number Program in Java Find Prime numbers between two numbers in Java Break statement in Java for each loop in Java Typecasting in Java Printing the format text with printf in Java How to generate random numbers within a range in Java Java Program to count all punctuation characters in the string Java program to print the following given pattern Java program to print the following given pattern Java program to print the following given pattern Java program to print the following pattern Java program to print the following pattern Java program to print the given pattern Java program to print the given pattern Java program to print the given pattern Java program to print the following pattern Java program to print the following pattern Java Program to Print the following Pattern Java program to print the following pattern Java Program to Print the following Pattern Java Program to print the smallest element in an array Java Program to Print the following pattern Java Program to Print the following pattern Java Program to Print the following Pattern Java Program to Copy One Array to Another in Java Java Program to find the frequency of each element in a array Java Program to left rotate the elements of an array Java Program to print the duplicate elements of an array Java Program to print the elements of an array Java Program for binary search Java Program for linear search Java Program for bubble sort Java Program for insertion sort Java Program for selection sort Java Program to print the elements of an array present on even position Java Program to print the elements of an array in reverse order Java Program to find Third Largest Number in an Array Java Program to print the largest element in an array Print the number of elements in an array java Java Program to print the sum of all the items of the array Java Program to right rotate the elements of an array Java Program to sort the elements of an array in ascending order Java Program to sort the elements of an array in descending order Java Program to print the elements of an array present on odd position Java Program to Check if it is a Sparse Matrix Java Program to check a given matrix is an identity matrix Java Program to determine whether two matrices are equal Java Program to display the lower triangular matrix Java Program to find the product of two matrices