Patterns in Java

Patterns Programs In Java: Star, Number & Character

You should know that different pattern programs can be developed using different programming languages. And Java programming language is no different from that. We can also develop patterns in Java using the for loops & conditional statements.

Pattern programs are the most important sector for questions in every interview to enter into the software development field. In this article, we will discuss 20+ pattern programs in Java programming language.

So, let us start our discussion.

How Many Types Of Pattern Programs Present In Java?

In every programming language, pattern programs can be implemented using different kinds of elements. Based on those elements, the pattern programs can be differentiated into three parts. In the case of Java, the same thing is applied.

The patterns in Java programming language can be divided into three categories:

  1. Java Character Patterns
  2. Java Special Character Patterns or Java Star Patterns
  3. Java Numeric Patterns

We will know about each of them one by one along with sample examples & their output.

How Many Pattern Programs Belong To Java Character Patterns?

In such pattern programs, alphabetic characters are used to develop the design. Sometimes, the alphabetic character increases or decreases in order along with the number of rows value.

We can classify it into four parts.

#1 Triangle Character Pattern Program:

public class Main
{
	public static void main(String[] args) {
	int a = 115; // ASCII Value Of Character
        
        for (int i = 0; i <= 2; i++) // Outer Loop
        {
            for (int j = 0; j <= i; j++) // Inner Loop
            {
                System.out.print((char) (a + j) + " "); // Print Pattern
            }
            System.out.println();
        }
    }
	}

Output:

Ouptut Of Triangle Shape Character Pattern Program

#2 Row-Wise Alphabetic Triangle Pattern:

public class Main
{
	public static void main(String[] args) {
	int a = 115; // ASCII Value Of Character
        
        for (int i = 0; i<= 2; i++) // Outer Loop
        {
            for (int j = 0; j <= i; j++) // Inner Loop
            {
                System.out.print((char) a + " "); // Print Pattern
            }
            a++;
            System.out.println();
        }
    }
	}

Output:

Row-Wise Alphabetic Triangle Pattern

#3 K-Style Alphabetic Character Pattern:

public class Main
{
	/* Program Starts With Public Static Void Main (String[] args)Function*/
	public static void main(String[] args) {
	for (int i = 2; i >= 0; i--) // Outer Loop For Upper Part
    {
        int a = 115; // ASCII Value Of Upper Character
        for (int j = 0; j <= i; j++) // Inner Loop For Upper Part
            System.out.print((char) (a + j) + " "); // Printing Value
        System.out.println();
    }
    
    for (int i = 0; i<= 2; i++) // Outer Loop For Lower Part
    {
        int b = 75; // ASCII Value Of Lower Character
        for (int j = 0; j <= i; j++) // Inner Loop For Lower Part
        {
            System.out.print((char) (b + j) + " "); // Print Pattern
        }
        System.out.println();
    }
}
}

Output:

K-Style Alphabetic Character Pattern

#4 Increasing Character In Triangle Pattern:

public class Main
{
	public static void main(String[] args) {
	for (int i = 0; i <= 2; i++) { // Outer Loop 
	    int a = 115; // ASCII Value Of Character
	    for (int j = 2; j > i; j--) // Inner Loop
            System.out.print(" ");
        for (int k = 0; k <= i; k++) // Inner Loop
            System.out.print((char) (a + k) + " "); // Print Pattern
        System.out.println();
    }
}
}

Output:

Increasing Character In Triangle Pattern

How Many Pattern Programs Belong To Java Special Character Or Star Patterns?

In such pattern programs, special characters like *,#,@,& are used to develop the design. In this case, we have implemented star pattern programs or special character pattern programs using the character “@”.

Using some loops & conditions such designs are built along with the help of the number of rows. We can classify it into eleven parts. If you face any difficulties learning about the below pattern programs then worry not, you can get Java Hw Help from CodingZap experts and clear your confusion.

#1 Hollow Diamond Shape Pattern Program:

public class Main
{
	public static void main(String[] args) { // Diamond Star Pattern Enter
	int r = 3; // Number Of Rows
    for (int i=1 ; i<= r ; i++)  // Outer Loop
    { 
        for (int j = r; j > i ; j--)
            System.out.print(" ");
        System.out.print("@");
        for (int k = 1; k < 2*(i -1) ;k++) 
            System.out.print(" "); 
        if(i==1) 
            System.out.println("");
        else
            System.out.println("@"); // Print Pattern
    } 
        for (int i=r-1; i>= 1 ; i--)
        {
            for (int j = r; j > i ; j--)
                System.out.print(" ");
            System.out.print("@"); // Print Pattern
            for (int k = 1; k < 2*(i -1) ;k++)
                System.out.print(" ");
        if(i==1)
            System.out.println("");
        else
            System.out.println("@"); // Print Pattern
    }
    }
}

Output:

Hollow Diamond Pattern Program

#2 Downward Special Character Triangle:

public class Main
{
    public static void main(String[] args) { // Down Triangle Star Pattern Enter
	int r = 3; // Number Of Rows
    for (int i=r; i>= 1 ; i--) // Outer Loop
    {
        for (int j = i; j < r ; j++)
            System.out.print(" ");
        for (int k = 1; k <= (2*i -1) ;k++) {
            if(k==1 || i == r || k == (2*i-1))
                System.out.print("@"); // Print Pattern
            else
                System.out.print(" ");
        }
        System.out.println("");
    }
    }
}

Output:

Java Pattern Program For Downward Special Character Triangle

#3 Upward Special Character Triangle:

public class Main
{
	/* Program Starts With Public Static Void Main (String[] args)Function*/
	public static void main(String[] args) {
	int r = 3; // Number of Rows  
    for (int i=1; i<= r ; i++) // Outer Loop
    {
        for (int j = i; j < r ; j++)
            System.out.print(" ");
        for (int k = 1; k <= (2*i -1) ;k++) {
            if( k==1 || i == r || k==(2*i-1))
                System.out.print("@"); // Print Pattern
            else
                System.out.print(" ");
        }
        System.out.println("");
    }
}
}
/* Here, the program also resembles with Pyramid star pattern or spceial character pattern */

Output:

Ouptut Of Upward Special Character Triangle

#4 Left Pascal’s Triangle Program With Special Characters:

public class Main
{
	/* Program Starts With Public Static Void Main (String[] args)Function*/
	public static void main(String[] args) {
	int r = 3; // Number Of Rows  
    for (int i= 1; i<= r ; i++) // Outer Loop
        {
            for (int j=i; j < r ;j++)            
                System.out.print(" ");
            for (int k=1; k <= i ; k++)
                System.out.print("@"); // Left Triangle Pascal's Enter
            System.out.println(""); 
        } 
        for (int i=r; i>=1; i--)
        {
            for(int j=i; j<=r;j++)
                System.out.print(" ");
            for(int k=1; k<i ;k++) 
                System.out.print("@"); // Left Triangle Pascal's Enter
            System.out.println("");
    }
}
}

Output:

Output Of Left Pascal's Triangle Program With Special Characters

#5 Right Pascal’s Triangle With Special Character:

public class Main
{
	/* Program Starts With Public Static Void Main (String[] args)Function*/
	public static void main(String[] args) {
	int r = 3; // Number Of Rows 
    for (int i= 0; i<= r-1 ; i++) // Outer Loop
        {
            for (int j=0; j<=i; j++) 
            { 
                System.out.print("@"); // Pattern Programs Enter
                System.out.print(" ");
            } 
            System.out.println(""); 
        } 
        for (int i=r-1; i>=0; i--)
        {
            for(int j=0; j <= i-1;j++)
            {
                System.out.print("@"); // Print Character
                System.out.print(" ");
            }
            System.out.println("");
        }
}
}

Output:

Output Of Right Pascal's Triangle With Special Character

#6 Reverse Pyramid Program With Special Character:

public class Main
{
	/* Program Starts With Public Static Void Main (String[] args)Function*/
	public static void main(String[] args) {
	int r = 3; // Number Of Rows Value  
    for (int i= 0; i<= r-1 ; i++) // Outer Loop
    {
        for (int j=0; j<=i; j++)
            System.out.print(" ");
        for (int k=0; k<=r-1-i; k++)
        {
            System.out.print("@"); // Printing Pattern
            System.out.print(" ");
        }
        System.out.println();
        } 
}}

Output:

Ouptut Of Reverse Pyramid Star Pattern Or Special Pattern

#7 Right Triangle Star Pattern Or Special Character Pattern:

public class Main
{
	// Triangle Star Program Enter, means starting field
	public static void main(String[] args) {
	int i;
	int j;
	int n = 3; // Number Of Rows  
    for(i=0; i<n; i++){  // Outer Loop
        for(j=0; j<=i; j++) //  Inner loop
            System.out.print("@" + " "); // Printing Pattern
        System.out.println();}
}}

Output:

Output Of Right Triangle Special Character Pattern

#8 Reverse Right Triangle Special Character Pattern:

public class Main
{
	/* Mirror Star Pattern Enter or Starting of Reverse Right Triangle Special Character Pattern */
	public static void main(String[] args) {
	int i;
	int j;
	int r = 3; // Number Of Rows  
    for (i = r-1; i>=0 ; i--) // Outer Loop
    {
        for (j = 0; j<=i; j++) // Inner Loop
            System.out.print("@ ");
        System.out.println();
}}}

Output:

Output Of Reverse Right Triangle Special Character Pattern

#9 Left Triangle Star Pattern Or Special Character Pattern (Mirror Of Right Angled):

public class Main
{
	public static void main(String[] args) {
	int r = 2; // Number Of rows  
    for (int i= 0; i<= r; i++) // Outer Loop
    {
        for (int j=1; j<=r-i; j++) // Inner Loop
            System.out.print(" ");
        for (int k=0;k<=i;k++) // Inner Loop
            System.out.print("@"); // Printing Pattern
        System.out.println();
    }    
    }}

Output:

Output Of Left Triangle Special Pattern

#10 Reverse Left Triangle Special Character Pattern:

public class Main
{
	public static void main(String[] args) {
	int r = 3; // Number Of Rows  
    for (int i = r; i>= 1; i--) // Outer Loop
    {
    for (int j = r; j>i; j--) // Inner Loop
        System.out.print(" ");
    for (int k=1;k<=i;k++) // Inner Loop
        System.out.print("@"); // Printing Pattern
    System.out.println();    
}
}}

Output:

Output Of Reverse Left Triangle Special Pattern

#11 Full Diamond Shape Pattern Program:

public class Main
{
	public static void main(String[] args) {
	int r = 2; // Number Of Rows
	int i;
	int j;
	int a = r-1; // Number Of Space
    for (j = 1; j<= r; j++) // Outer Loop For Upper Part
    {
        for (i = 1; i<= a; i++) // Inner Loop For Upper Part
            System.out.print(" ");
        a--;
        for (i = 1; i <= 2 * j - 1; i++) // Inner Loop For Upper Part
            System.out.print("@");
        System.out.println();
    }
    a = 1;
    for (j = 1; j<= r - 1; j++) // Outer Loop For Lower Part
    {
        for (i = 1; i<= a; i++) // Inner Loop For Lower Part
            System.out.print(" ");
        a++;
        for (i = 1; i<= 2 * (r - j) - 1; i++) // Inner Loop For Lower Part
            System.out.print("@");
    System.out.println();
}
}}

Output:

Output Of Full Diamond Shape Pattern

How Many Pattern Programs Belong To Java Character Patterns?

The number pattern program is defined as the series of numbers used to make the design. The numbers might get increased along with the number of rows inside the program. Sometimes, we might go for a binary number pattern entry program also.

The article precisely focuses on a total of six number pattern programs.

#1 Increasing Right Triangle Numeric Pattern:

public class Main
{
	public static void main(String[] args) {
	int n = 3; // Number Of Rows
	int a; // Declartion Of Variable int a
	int i; // Declartion Of Variable int i
	int j; // Declartion Of Variable int j
	for(i=0; i<n; i++) // Outer Loop
    { 
        a = 1; 
        for(j=0; j<=i; j++) // Inner Loop
        { 
            System.out.print(a + " "); // No Pattern Void Display Function used To Print
            a++; 
        } 
        System.out.println(); 
    } 
}}

Output:

Output Of Increasing Numeric Right Angled Triangle

#2 Decreasing Right Triangle Numeric Pattern:

public class Main
{
	/* Triangle Numeric Pattern Enter or Starting of The Pattern Program*/
	public static void main(String[] args) {
	int n = 3; // Number Of Rows
	int i; // Declartion Of Variable int i
	int j; // Declartion Of Variable int j
	for (i = n; i >= 1; i--) // Outer Loop
    {
        for (j = n; j >= i; j--) // Inner Loop
            System.out.print(j+" "); // Printing Pattern
        System.out.println();
    } 
}}

Output:

Output Of Decreasing Numeric Right Angled Triangle

#3 Pascal Number Triangle Pattern:

public class Main
{
	public static void main(String[] args) {
	int n = 3; // Number Of Rows
	int i; // Declartion Of Variable int i
	int j; // Declartion Of Variable int j
	for (i = 0; i < n; i++) { // Outer Loop
        int a = 1; // Space Number
        System.out.printf("%" + (n - i) * 2 + "s", ""); // Printing Pattern
        for (j = 0; j <= i; j++) {
            System.out.printf("%4d", a); // Printing Pattern
            a = a * (i - j) / (j + 1); 
        }
        System.out.println();
    } 
}}

Output:

Output Of Pascal Number Triangle Pattern

#4 Each Number In Each Row Pattern:

public class Main
{
	public static void main(String[] args) {
	int n = 3; // Number Of Rows
	int i; // Declartion Of Variable int i
	int j; // Declartion Of Variable int j
	for (i = 1; i <= n; i++) // Outer Loop
    {
        for (j = 1; j <= i; j++) // Inner Loop
            System.out.print(i+" "); // Printing Pattern
        System.out.println();
    } 
}}

Output:

Output Of Each Number In Each Row Pattern

#5 Row Wise Decreasing Pattern:

public class Main
{
	/* Descending Order Pattern Enter Point*/
	public static void main(String[] args) {
	int n = 3; // Number Of Rows
	int i; // Declartion Of Variable int i
	int j; // Declartion Of Variable int j
	for (i = 1; i <= n; i++) // Outer Loop
	{ 
	   for (j = i; j >= 1; j--) // Inner Loop
            System.out.print(j+" "); // Printing Pattern
        System.out.println();
    } 
}}

Output:

Output Of Row Wise Decreasing Pattern

#6 Sandglass Number Pattern:

public class Main
{
	/* Sandglass Star Pattern Enter & Starting Point of Program*/
	public static void main(String[] args) {
	int n = 2; // Number Of Rows
	for (int i = 1; i <= n; i++) // Outer Loop For Upper Part
        {
            for (int j = 1; j < i; j++) // Inner Loop For Upper Part
                System.out.print(" ");
            for (int k = i; k <= n; k++) // Inner Loop For Upper Part
                System.out.print(k+" ");
            System.out.println(); 
        }
        
        for (int i = n-1; i >= 1; i--) // Outer Loop For Lower Part
        {
            for (int j = 1; j < i; j++) // Inner Loop For Lower Part
                System.out.print(" ");
            for (int k = i; k <= n; k++) // Inner Loop For Lower Part
                System.out.print(k+" ");
            System.out.println();
        } 
}}

Output:

Output Of Sandglass Number Pattern

Conclusion:

From the above discussion we hope, the concept of pattern programs in Java is much more clear to you.

All the sample programs are implemented by taking the Public Static Void Main function as the base. We have not declared any special user-defined function besides the Public Static Void Main function. You should remember that.

We advise you to focus on the basic concept of Java programming language, more specifically on loops & conditional statements. Because the pattern programs are the result of particular usage of such concepts. Loops is an interesting concept and loop has different features in each programming language. Like, a loop in Python is used for iteration. If you get the condition for implementing that, it will become one cakewalk for you. If you want to know more about patterns in Java, then you can read out our article on MVC architecture in Java and know about patterns.

Leave a Comment

Your email address will not be published. Required fields are marked *