Types of Pattern Programs in Java
The patterns can be classified as :
- Star Patterns
- Numeric Patterns
- Character Patterns
There can be two or more loops in each pattern program. The number of loops required is determined by the pattern's complexity. The first loop affects the row, whereas the second affects the column.
Star Patterns
Simple Pyramid Pattern
The following code illustrates printing a simple pyramid pattern:
Java
public class Ninja {
public static void main(String ar[])
{
int length = 6;
for (int i=0; i<length; i++) // loop for number of rows
{
for (int j=length-i; j>1; j--) // loop for spaces
{
System.out.print(" "); //print space
}
for (int j=0; j<=i; j++ ) // loop for number of columns
{
System.out.print("* "); //printing star
}
System.out.println(); //ending line
}
}
}

You can also try this code with Online Java Compiler
Run Code
File name : Ninja.java
Output :
*
* *
* * *
* * * *
* * * * *
* * * * * *
Left Triangle Star Pattern
The following code illustrates printing a Left Triangle Star pattern:
Java
public class Ninja {
public static void main(String ar[])
{
int j,length=6;
for(int i=0; i<length; i++) //loop for number of rows
{
for( j=2*(length-i); j>=0; j--) // loop for spaces
{
System.out.print(" "); // printing space
}
for(j=0; j<=i; j++) //loop for columns
{
System.out.print("* "); // printing star
}
System.out.println(); // ending line
}
}
}

You can also try this code with Online Java Compiler
Run Code
File name : Ninja.java
Output :
*
* *
* * *
* * * *
* * * * *
* * * * * *
Right Triangle Star Pattern
The following code illustrates printing a Right Triangle Star pattern:
Java
public class Ninja {
public static void main(String ar[])
{
int i, j , length =6;
for(i=0; i<length; i++) //loop to handle number of rows
{
for(j=0; j<=i; j++)
{
System.out.print("*"); // printing stars
}
System.out.println(); // ending line
}
}
}

You can also try this code with Online Java Compiler
Run Code
File name : Ninja.java
Output :
*
**
***
****
*****
******
Try it by yourself on Java Online Compiler.
Downward Triangle Star Pattern
The following code illustrates printing a Downward Triangle Star pattern:
Java
public class Ninja {
public static void main(String ar[])
{
int length=6;
for (int i=length-1; i>=0 ; i--) //loop for rows
{
for (int j=0; j<=i; j++) //loop for columns
{
System.out.print("*"); //printing stars
}
System.out.println(); //ending line
}
}
}

You can also try this code with Online Java Compiler
Run Code
File name : Ninja.java
Output :
******
*****
****
***
**
*
Right Pascal’s Triangle
The following code illustrates printing Right Pascal’s Triangle :
Java
public class Ninja {
public static void main(String ar[])
{
int length=6;
for (int i= 0; i<= length-1 ; i++) //loop for rows
{
for (int j=0; j<=i; j++) //loop for columns
{
System.out.print("*"); //printing stars
}
System.out.println("");
}
for (int i=length-1; i>=0; i--)
{
for(int j=0; j <= i-1;j++)
{
System.out.print("*"); //printing stars
}
System.out.println("");
}
}
}

You can also try this code with Online Java Compiler
Run Code
File name : Ninja.java
Output:
*
**
***
****
*****
******
*****
****
***
**
*
Left Pascal’s Triangle
The following code illustrates printing Left Pascal’s Triangle :
Java
public class Ninja {
public static void main(String ar[])
{
int length=6;
for (int i= 1; i<= length ; i++)
{
for (int j=i; j <length ;j++)
{
System.out.print(" ");
}
for (int k=1; k<=i;k++)
{
System.out.print("*");
}
System.out.println("");
}
for (int i=length; i>=1; i--)
{
for(int j=i; j<=length;j++)
{
System.out.print(" ");
}
for(int k=1; k<i ;k++)
{
System.out.print("*");
}
System.out.println("");
}
}
}

You can also try this code with Online Java Compiler
Run Code
File name : Ninja.java
Output:
*
**
***
****
*****
******
*****
****
***
**
*
Triangle Star Pattern
The following code illustrates printing Triangle Star Pattern :
Java
public class Ninja {
public static void main(String ar[])
{
int length=6;
for (int i=1; i<= length ; i++)
{
for (int j = i; j < length ; j++) {
System.out.print(" ");
}
for (int k = 1; k <= (2*i -1) ;k++) {
if( k==1 || i == length || k==(2*i-1)) {
System.out.print("*");
}
else {
System.out.print(" ");
}
}
System.out.println("");
}
}
}

You can also try this code with Online Java Compiler
Run Code
File name : Ninja.java
Output:
*
* *
* *
* *
* *
***********
Reverse Triangle Star Pattern
The following code illustrates printing Reverse Triangle Star Pattern :
Java
public class Ninja {
public static void main(String ar[])
{
int length=6;
for (int i=length; i>= 1 ; i--)
{
for (int j = i; j < length ; j++) {
System.out.print(" ");
}
for (int k = 1; k <= (2*i -1) ;k++) {
if( k==1 || i == length || k==(2*i-1)) {
System.out.print("*");
}
else
{
System.out.print(" ");
}
}
System.out.println("");
}
}
}

You can also try this code with Online Java Compiler
Run Code
File name : Ninja.java
Output:
***********
* *
* *
* *
* *
*
Sandglass Star Pattern
The following code illustrates printing Sandglass Star Pattern :
Java
public class Ninja {
public static void main(String ar[])
{
int length=6;
for (int i= 0; i<= length-1 ; i++)
{
for (int j=0; j <i; j++)
{
System.out.print(" ");
}
for (int k=i; k<=length-1; k++)
{
System.out.print("* ");
}
System.out.println("");
}
for (int i= length-1; i>= 0; i--)
{
for (int j=0; j< i ;j++)
{
System.out.print(" ");
}
for (int k=i; k<=length-1; k++)
{
System.out.print("* ");
}
System.out.println("");
}
}
}

You can also try this code with Online Java Compiler
Run Code
File name : Ninja.java
Output:
* * * * * *
* * * * *
* * * *
* * *
* *
*
*
* *
* * *
* * * *
* * * * *
* * * * * *
Numeric Patterns
Number Pattern
The following code illustrates printing Number Pattern :
Java
public class Ninja {
public static void main(String ar[])
{
int i, j,number;
int row = 6;
for(i=0; i<row; i++) // loop for rows
{
number=1;
for(j=0; j<=i; j++) // loop for rows
{
System.out.print(number); // printing number
number++; //incrementing value of num
}
System.out.println(); // ending line
}
}
}

You can also try this code with Online Java Compiler
Run Code
File name : Ninja.java
Output:
1
12
123
1234
12345
123456
Incrementing Number Pattern
The following code illustrates printing Increment Number Pattern :
Java
public class Ninja {
public static void main(String ar[])
{
int i, j,number=1;
int row = 3;
for(i=0; i<row; i++) // loop for rows
{
for(j=0; j<=i; j++) // loop for rows
{
System.out.print(number); // printing num
number++; //incrementing value of num
}
System.out.println(); // ending line
}
}
}

You can also try this code with Online Java Compiler
Run Code
File name : Ninja.java
Output:
1
23
456
Descending order Pattern
The following code illustrates Descending order Pattern :
Java
public class Ninja {
public static void main(String ar[])
{
int row = 6;
for (int i = row; i >= 1; i--)
{
for (int j = row; j >= i; j--)
{
System.out.print(j);
}
System.out.println();
}
}
}

You can also try this code with Online Java Compiler
Run Code
File name : Ninja.java
Output:
6
65
654
6543
65432
654321
Binary Number Pattern
The following code illustrates printing Binary Number Pattern :
Java
public class Ninja {
public static void main(String ar[])
{
int rows=4;
for (int i = 1; i <= rows; i++)
{
int num;
if(i%2 == 0)
{
num = 0;
for (int j = 1; j <= rows; j++)
{
System.out.print(num);
num = (num == 0)? 1 : 0;
}
}
else
{
num = 1;
for (int j = 1; j <= rows; j++)
{
System.out.print(num);
num = (num == 0)? 1 : 0;
}
}
System.out.println();
}
}
}

You can also try this code with Online Java Compiler
Run Code
File name : Ninja.java
Output:
1010
0101
1010
0101
Diamond Numeric Pattern
The following code illustrates printing Diamond Pattern :
Java
public class Ninja {
public static void main(String ar[])
{
int length = 5;
for (int i = 1; i <= length; i++)
{
for (int j = 1; j < i; j++)
{
System.out.print(" ");
}
for (int k = i; k <= length; k++)
{
System.out.print(k+" ");
}
System.out.println();
}
for (int i = length-1; i >= 1; i--)
{
for (int j = 1; j < i; j++)
{
System.out.print(" ");
}
for (int k = i; k <= length; k++)
{
System.out.print(k+" ");
}
System.out.println();
}
}
}

You can also try this code with Online Java Compiler
Run Code
File name : Ninja.java
Output:
1 2 3 4 5
2 3 4 5
3 4 5
4 5
5
4 5
3 4 5
2 3 4 5
1 2 3 4 5
Also check out Addition of Two Numbers in Java here.
Character Patterns
Right Alphabetic triangle
The following code illustrates printing Right Alphabetic triangle:
Java
public class Ninja {
public static void main(String ar[])
{
char alphabet = 'A';
for (int i = 0; i <= 5; i++) //loop for rows
{
for (int j = 0; j <= i; j++) //loop for columns
{
System.out.print((char) (alphabet+j)); //printing Alphabet
}
System.out.println();
}
}
}

You can also try this code with Online Java Compiler
Run Code
File name : Ninja.java
Output:
A
AB
ABC
ABCD
ABCDE
ABCDEF
Incrementing Right Alphabetic triangle
The following code illustrates printing Incrementing Right Alphabetic triangle:
Java
public class Ninja {
public static void main(String ar[])
{
char alphabet = 'A';
for (int i = 0; i <= 5; i++) //loop for rows
{
for (int j = 0; j <= i; j++) //loop for columns
{
System.out.print((char) (alphabet++)); //printing Alphabet with increment
}
System.out.println();
}
}
}

You can also try this code with Online Java Compiler
Run Code
File name : Ninja.java
Output:
A
BC
DEF
GHIJ
KLMNO
PQRSTU
Row Incrementing Right Alphabetic triangle
The following code illustrates printing Row Incrementing Right Alphabetic triangle:
Java
public class Ninja {
public static void main(String ar[])
{
char alphabet = 'A';
for (int i = 0; i <= 5; i++) //loop for rows
{
for (int j = 0; j <= i; j++) //loop for columns
{
System.out.print((char) (alphabet)); //printing Alphabet with increment
}
alphabet++;
System.out.println();
}
}
}

You can also try this code with Online Java Compiler
Run Code
File name : Ninja.java
Output:
A
BB
CCC
DDDD
EEEEE
K Shape Character Pattern
The following code illustrates printing K Shape Character Pattern:
Java
public class Ninja {
public static void main(String ar[])
{
for (int i = 5; i >= 0; i--)
{
char alphabet = 'A';
for (int j = 0; j <= i; j++)
{
System.out.print((char) (alphabet + j));
}
System.out.println();
}
for (int i = 0; i<= 5; i++)
{
char alphabet = 'A';
for (int j = 0; j <= i; j++)
{
System.out.print((char) (alphabet + j));
}
System.out.println();
}
}
}

You can also try this code with Online Java Compiler
Run Code
File name : Ninja.java
Output:
ABCDEF
ABCDE
ABCD
ABC
AB
A
A
AB
ABC
ABCD
ABCDE
ABCDEF
You can also read about java destructor.
Must Read: Java System Out Println
Frequently Asked Questions
What is the use of pattern in Java?
Patterns in Java are used to print specific arrangements of characters, numbers, or symbols in a structured format. They are commonly used for decorative purposes, formatting output, or solving programming challenges involving repetitive sequences.
How to solve pattern problems in Java?
To solve pattern problems in Java, you typically use nested loops to control the rows and columns of the pattern. By strategically using loops and conditional statements, you can generate the desired pattern by printing the appropriate characters or numbers in each iteration.
How to print string pattern in Java?
To print string patterns in Java, you can use a combination of loops and string manipulation techniques. Iterate over each row and column of the pattern, concatenating the appropriate strings or characters to form the desired output.
What is pattern printing?
Pattern printing refers to the process of generating and printing specific arrangements of characters, numbers, or symbols in a structured format. Patterns can range from simple shapes like triangles and squares to complex designs, and they are commonly used in programming challenges and decorative output.
Conclusion
In this article, we have extensively discussed printing different patterns in Java. The article explains the different types of pattern programs namely star patterns , numeric patterns and character patterns with their program to print them. These types of programming questions are very important for interview preparation purposes.
Recommended Readings:
We hope that this blog has helped you enhance your knowledge regarding the reverse of string and if you would like to learn more, check out our articles on Java. Do upvote our blog to help other ninjas grow.
Happy Coding!