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:

0
909
89098
7890987
678909876
56789098765
4567890987654
345678909876543
23456789098765432
1234567890987654321

In this example we will see a Java program in which we will try to print the above given pattern on the console.

Algorithm:
  • Step 1:Start.
  • Step 2:Set lines=10 and i=1.
  • Step 3:Repeat step 4 to step 15 until i<=lines.
  • Step 4:Set count =0 and j=1.
  • Step 5:Repeat step 6 and step 7 until j<lines< li=""></lines<>.
  • Step 6:IF count is less than (i-1)
    IF j is not less than (lines-i+1)
    PRINT j and increment count by 1.
  • Step 7:Set j=j+1.
  • Step 8:PRINT 0.
  • Step 9:Set count =0.
  • Step 10:Decrement j by 1.
  • Step 11:Repeat step 12 and step 13 until j is greater than 0.
  • Step 12:IF count is less than (i-1)
    PRINT j and increment count by 1.
  • Step 13:Set j = j – 1.
  • Step 14:PRINT a new line.
  • Step 15:i = i + 1.
  • Step 16:End.
Program:

public class Main
{  
public static void main(String []args)
{  
int lines=10;  
int i,j;  
int count;  
for(i=1;i<=lines;i++)
{
count=0;  
for(j=1;j<lines;j++)
{ 
if(count<i-1){  
if(!(j<lines-i+1))
{  
System.out.print(j);  
count++;  
}  
}  
}  
System.out.print("0");  
count=0;  
for(--j;j>=1;j--){ 
if(count<i-1){  
System.out.print(j);  
count++;  
}  
}  
System.out.println("");  
}  
}  
}  

Output:
0
909
89098
7890987
678909876
56789098765
4567890987654
345678909876543
23456789098765432
1234567890987654321

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