Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Java Pattern Programs
3.
Types of Pattern Programs in Java
4.
Star Patterns
4.1.
Simple Pyramid Pattern
4.2.
Java
4.3.
Left Triangle Star Pattern
4.4.
Java
4.5.
Right Triangle Star Pattern
4.6.
Java
4.7.
Downward Triangle Star Pattern
4.8.
Java
4.9.
Right Pascal’s Triangle
4.10.
Java
4.11.
Left Pascal’s Triangle
4.12.
Java
4.13.
Triangle Star Pattern
4.14.
Java
4.15.
Reverse Triangle Star Pattern
4.16.
Java
4.17.
Sandglass Star Pattern
4.18.
Java
5.
Numeric Patterns
5.1.
Number Pattern
5.2.
Java
5.3.
Incrementing Number Pattern
5.4.
Java
5.5.
Descending order Pattern
5.6.
Java
5.7.
Binary Number Pattern
5.8.
Java
5.9.
Diamond Numeric Pattern
5.10.
Java
6.
Character Patterns
6.1.
Right Alphabetic triangle
6.2.
Java
6.3.
Incrementing Right Alphabetic triangle
6.4.
Java
6.5.
Row Incrementing Right Alphabetic triangle
6.6.
Java
6.7.
K Shape Character Pattern
6.8.
Java
7.
Frequently Asked Questions
7.1.
What is the use of pattern in Java?
7.2.
How to solve pattern problems in Java?
7.3.
How to print string pattern in Java?
7.4.
What is pattern printing?
8.
Conclusion
Last Updated: May 17, 2024
Easy

Printing Pattern in Java

Author Nagendra
1 upvote
Crack Google SDE interview : Essential projects
Speaker
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM

Introduction

Pattern programs in Java are a popular way to sharpen coding skills and enhance problem-solving abilities. These programs involve printing specific patterns of characters, numbers, or symbols in a structured format. Whether you're a beginner looking to understand the basics of loops and conditional statements or an experienced programmer aiming to polish your logical thinking, pattern programs offer valuable practice.

In this blog, we'll explore various types of pattern programs in Java, ranging from simple shapes to complex designs.

Java Pattern Programs

Java Pattern Programs

Java pattern programs involve printing specific patterns of characters, numbers, or symbols in a structured format. These programs are an excellent way to practice fundamental programming concepts such as loops, conditional statements, and nested structures. They help improve logical thinking, problem-solving skills, and code readability. Pattern programs come in various complexities, from simple geometric shapes to intricate designs, making them suitable for programmers of all levels.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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

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
       }
   }
}

File name : Ninja.java

Output :

     * 
    * * 
   * * * 
  * * * * 
 * * * * * 
* * * * * * 

Left Triangle Star Pattern

The following code illustrates printing a Left Triangle Star pattern:

  • Java

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
       }
   }
}

File name : Ninja.java

Output :

             * 
           * * 
         * * * 
       * * * * 
     * * * * * 
   * * * * * * 

Right Triangle Star Pattern

The following code illustrates printing a Right Triangle Star pattern:

  • Java

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
         }
   }
}

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

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
       }
   }
}

File name : Ninja.java

Output :

******
*****
****
***
**
*

Right Pascal’s Triangle

The following code illustrates printing Right Pascal’s Triangle :

  • Java

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("");
       }
   }
}

File name : Ninja.java

Output:

*
**
***
****
*****
******
*****
****
***
**
*

Left Pascal’s Triangle

The following code illustrates printing Left Pascal’s Triangle :

  • Java

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("");
       }
   }
}

File name : Ninja.java

Output:

     *
    **
   ***
  ****
 *****
******
 *****
  ****
   ***
    **
     *

Triangle Star Pattern

The following code illustrates printing Triangle Star Pattern :

  • Java

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("");
           }
   }
}

File name : Ninja.java

Output:
     *
    * *
   *   *
  *     *
 *       *
***********

Reverse Triangle Star Pattern

The following code illustrates printing Reverse Triangle Star Pattern :

  • Java

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("");
       }
   }
}

File name : Ninja.java

Output:

***********
 *       *
  *     *
   *   *
    * *
     *

Sandglass Star Pattern

The following code illustrates printing Sandglass Star Pattern :

  • Java

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("");
       }
   }
}

File name : Ninja.java

Output:

* * * * * * 
 * * * * * 
  * * * * 
   * * * 
    * * 
     * 
     * 
    * * 
   * * * 
  * * * * 
 * * * * * 
* * * * * * 

Numeric Patterns

Number Pattern

The following code illustrates printing Number Pattern :

  • Java

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
       }
   }
}

File name : Ninja.java
Output:

1
12
123
1234
12345
123456

Incrementing Number Pattern

The following code illustrates printing Increment Number Pattern :

  • Java

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
       }
   }
}

File name : Ninja.java

Output:

1
23
456

Descending order Pattern

The following code illustrates Descending order Pattern :

  • Java

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();
       }
   }
}

File name : Ninja.java

Output:
6
65
654
6543
65432
654321

Binary Number Pattern

The following code illustrates printing Binary Number Pattern :

  • Java

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();
       }         
   }
}

File name : Ninja.java

Output:

1010
0101
1010
0101

Diamond Numeric Pattern

The following code illustrates printing Diamond Pattern :

  • Java

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();
       }
                 
   }
}

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

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();
       }
   }
}

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

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();
       }
   }
}

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

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();
       }
   }
}

File name : Ninja.java

Output:
A
BB
CCC
DDDD
EEEEE

K Shape Character Pattern

The following code illustrates printing K Shape Character Pattern:

  • Java

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();
      }
   }
}

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!

Previous article
Java Program to Check Armstrong Number
Next article
Java Program to Check if a String is Palindrome
Live masterclass