Java/J2EE and SOA (348 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (16 Blogs)
  • Comprehensive Java Course (4 Blogs)
  • Java/J2EE and SOA (345 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

Top 30 Pattern Program in Java: How to Print Star, Number and Character

Last updated on Apr 25,2024 788.8K Views

Aayushi Johari
A technophile with a passion for unraveling the intricate tapestry of the... A technophile with a passion for unraveling the intricate tapestry of the tech world. I've spent over a decade exploring the fascinating world of...
14 / 29 Blog from Java Programs

Java Interviews can be difficult for programmers, given the severity of the process. Those who have attended the process will know that a Java pattern program ought to pop up in the list of programs.

This article precisely focuses on pattern programs in Java. I have classified the programs under the following clusters :

Pattern Programs in Java

Let’s get started. :-)

Star Patterns in Java

First, let us begin with the basic and commonly asked pattern program in Java i.e Pyramid.

1. Pyramid Program

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

Let’s write the java code to understand this pattern better.

public class Edureka
{  
    public static void pyramidPattern(int n) 
    {  
        for (int i=0; i<n; i++) //outer loop for number of rows(n) { for (int j=n-i; j>1; j--) //inner loop for spaces
            { 
                System.out.print(" "); //print space
            }  
            for (int j=0; j<=i; j++ ) //inner loop for number of columns
            { 
                System.out.print("* "); //print star
            } 
 
            System.out.println(); //ending line after each row
        } 
    } 
 
    public static void main(String args[]) //driver function
    { 
        int n = 5; 
        pyramidPattern(n); 
    } 
}

 

2. Right Triangle Star Pattern

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

public class Edureka 
{ 
    public static void rightTriangle(int n) 
    { 
        int i, j;  
        for(i=0; i<n; i++) //outer loop for number of rows(n) { for(j=2*(n-i); j>=0; j--) // inner loop for spaces 
            {           
                System.out.print(" "); // printing space
            } 
            for(j=0; j<=i; j++) //  inner loop for columns
            {       
                System.out.print("* "); // print star
            }           
            System.out.println(); // ending line after each row
        } 
    } 
    public static void main(String args[]) 
    { 
        int n = 5; 
        rightTriangle(n); 
    } 
}

3. Left Triangle Star Pattern

           * 
         * * 
       * * * 
     * * * * 
   * * * * * 
public class Edureka 
{ 
    public static void printStars(int n) 
    { 
        int i, j;  
        for(i=0; i<n; i++) //outer loop for number of rows(n) { for(j=2*(n-i); j>=0; j--) // inner loop for spaces 
            {           
                System.out.print(" "); // printing space
            } 
            for(j=0; j<=i; j++) //  inner loop for columns
            {       
                System.out.print("* "); // print star
            }           
            System.out.println(); // ending line after each row
        } 
    } 
    public static void main(String args[]) 
    { 
        int n = 5; 
        printStars(n); 
    } 
}

4. Diamond Shape Pattern Program in Java

Enter the number of rows: 5

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

import java.util.Scanner;
public class Edureka
{
public static void main(String args[])
{
int n, i, j, space = 1;
System.out.print("Enter the number of rows: ");
Scanner s = new Scanner(System.in);
n = s.nextInt();
space = n - 1;
for (j = 1; j<= n; j++)
{
for (i = 1; i<= space; i++)
{
System.out.print(" ");
}
space--;
for (i = 1; i <= 2 * j - 1; i++)
{
System.out.print("*");
}
System.out.println("");
}
space = 1;
for (j = 1; j<= n - 1; j++)
{
for (i = 1; i<= space; i++)
{
System.out.print(" ");
}
space++;
for (i = 1; i<= 2 * (n - j) - 1; i++)
{
System.out.print("*");
}
System.out.println("");
}
}
}

5. Downward Triangle Star Pattern

Enter the number of rows: 5

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

import java.util.Scanner;
public class Edureka
{
	public static void main(String[] args)
	{
	Scanner sc = new Scanner(System.in);

	System.out.println("Enter the number of rows: "); //takes input from user

	int rows = sc.nextInt();

	for (int i= rows-1; i>=0 ; i--)
	{
	for (int j=0; j<=i; j++)
	{
	System.out.print("*" + " ");
	}
	System.out.println();
	}
	sc.close();
	}
	} 

6. Mirrored Right Triangle Star Program

Enter the number of rows: 5

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

import java.util.Scanner;
public class Edureka
{
	public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);

        System.out.println("Enter number of rows: "); // takes input from user

        int rows = sc.nextInt();
        
        for (int i= 0; i<= rows; i++)
        {
            for (int j=1; j<=rows-i; j++)
            {
                System.out.print(" ");
            }
            for (int k=0;k<=i;k++)
            {
                System.out.print("*");
            } 
                System.out.println("");
        }
        sc.close();

    }
}

7. Reversed Pyramid Star Pattern

Enter the number of rows: 5

 * * * * * 
  * * * * 
   * * * 
    * * 
     * 
import java.util.Scanner;
public class Edureka
{
	public static void main(String[] args)
{
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter the number of rows: ");

    int rows = sc.nextInt();        
    for (int i= 0; i<= rows-1 ; i++)
    {
        for (int j=0; j<=i; j++)
        {
            System.out.print(" ");
        }
        for (int k=0; k<=rows-1-i; k++)
        {
            System.out.print("*" + " ");
        }
        System.out.println();
    }
    sc.close();

}
}

8. Right down Mirror Star Pattern
Enter the number of rows: 5

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

import java.util.Scanner;
public class Edureka
{

	public static void main(String[] args)
	{
	Scanner sc = new Scanner(System.in); // takes input
	System.out.println("Enter number of rows: ");
	int rows = sc.nextInt();
	for (int i= rows; i>= 1; i--)
	{
	for (int j=rows; j>i;j--)
	{
	System.out.print(" ");
	}
	for (int k=1;k<=i;k++)
	{
	System.out.print("*");
	}
	System.out.println("");
	}
	sc.close();
	}
	}

 

9. Right Pascal’s Triangle

Enter the number of rows: 5

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

 


import java.util.Scanner;
public class Edureka
{
	public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number of rows: ");

        int rows = sc.nextInt();        
        for (int i= 0; i<= rows-1 ; i++)
        {
            for (int j=0; j<=i; j++) { System.out.print("*"+ " "); } System.out.println(""); } for (int i=rows-1; i>=0; i--)
        {
            for(int j=0; j <= i-1;j++)
            {
                System.out.print("*"+ " ");
            }
            System.out.println("");
        }
        sc.close();
    }
}

10. Left  Triangle Pascal’s

Enter the number of rows: 5

    *
   **
  ***
 ****
*****
 ****
  ***
   **
    *
import java.util.Scanner;
public class Edureka
{

	public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number of rows: ");

        int rows = sc.nextInt();        
        for (int i= 1; i<= rows ; i++)
        {
            for (int j=i; j <rows ;j++)            
        {
                System.out.print(" ");
            }
            for (int k=1; k<=i;k++) { System.out.print("*"); } System.out.println(""); } for (int i=rows; i>=1; i--)
        {
            for(int j=i; j<=rows;j++)
            {
                System.out.print(" ");
            }
            for(int k=1; k<i ;k++) 
            {
                System.out.print("*");
            }
            System.out.println("");

        }
        sc.close();
    }
}

 

11. Sandglass Star Pattern

Enter the number of rows: 5

* * * * * 
 * * * * 
  * * * 
   * * 
    * 
    * 
   * * 
  * * * 
 * * * * 
* * * * * 
import java.util.Scanner;
public class Edureka
{
	public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number of rows: ");

        int rows = sc.nextInt();	        
        for (int i= 0; i<= rows-1 ; i++)
        {
            for (int j=0; j <i; j++)
            {
                System.out.print(" ");
            }
            for (int k=i; k<=rows-1; k++) { System.out.print("*" + " "); } System.out.println(""); } for (int i= rows-1; i>= 0; i--)
        {
            for (int j=0; j< i ;j++)
            {
                System.out.print(" ");
            }
            for (int k=i; k<=rows-1; k++)
            {
                System.out.print("*" + " ");
            }
            System.out.println("");
        }
        sc.close();
    }
}

12. Alphabet A Pattern

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

 

import java.util.Scanner;
public class Edureka
{
	// Java program to print alphabet A pattern
	void display(int n)
	{
	// Outer for loop for number of lines
	for (int i = 0; i<=n; i++) {
	// Inner for loop for logic execution
	for (int j = 0; j<= n / 2; j++) {
	// prints two column lines
	if ((j == 0 || j == n / 2) && i != 0 ||
	// print first line of alphabet
	i == 0  && j != n / 2 ||
	// prints middle line
	i == n / 2)
	System.out.print("*");
	else
	System.out.print(" ");
	}
	System.out.println();
	}
	}
	public static void main(String[] args)
	{
	Scanner sc = new Scanner(System.in);
	Edureka a = new Edureka();
	a.display(7);
	}
	}

13. Triangle Star pattern

Enter the number of rows: 5

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

 


import java.util.Scanner;
public class Edureka
{
	 public static void main(String[] args)
	    {
	        Scanner sc = new Scanner(System.in);

	        System.out.println("Enter the number of rows: ");

	        int rows = sc.nextInt();
	        
	        for (int i=1; i<= rows ; i++)
	        {
	            for (int j = i; j < rows ; j++) {
	                System.out.print(" ");
	            }   
	            for (int k = 1; k <= (2*i -1) ;k++) {
	                if( k==1 || i == rows || k==(2*i-1)) {
	                    System.out.print("*");
	                }
	                else {
	                    System.out.print(" ");
	                }
	            }
	            System.out.println("");
	        }
	        sc.close();
	    }
	}

 

14. Down triangle

Enter the number of rows: 5

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

 


import java.util.Scanner;
public class Edureka
{
public static void main(String[] args)
{
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter the number of rows: ");

    int rows = sc.nextInt();    
     for (int i=rows; i>= 1 ; i--)
    {
        for (int j = i; j < rows ; j++) {
            System.out.print(" ");
        }   
        for (int k = 1; k <= (2*i -1) ;k++) {
            if( k==1 || i == rows || k==(2*i-1)) {
                System.out.print("*");
            }
            else {
                System.out.print(" ");
            }
        }
        System.out.println("");
    }
    sc.close();
}
}

15. Diamond Star Pattern

Enter the number of rows: 5

    *
   * *
  *   *
 *     *
*       *
 *     *
  *   *
   * *
    *
import java.util.Scanner;
public class Edureka
{
	public static void main(String[] args)
{
    Scanner sc = new Scanner(System.in);

    System.out.println("Enter the number of rows: ");

    int rows = sc.nextInt();    
    for (int i=1; i<= rows ; i++) { for (int j = rows; 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("*"); } } for (int i=rows-1; i>= 1 ; i--)
        {
        for (int j = rows; 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("*");
    }
    sc.close();
}
}

Java Full Course in 10 Hours | Java Tutorial for Beginners [2024] | Java Online Training | Edureka

This Edureka Java Full Course will help you in understanding the various fundamentals of Java Programming and also helps you to became a master in Java concepts.

Now that we have implemented star pattern program in Java. Let us move further and implement some Numeric patterns.

Numeric Pattern in Java

 

1. Simple number program

1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5

 


public class Edureka
{
	public static void printNums(int n) 
    { 
        int i, j,num; 
   
        for(i=0; i<n; i++) // outer loop for rows
        { 
            num=1; 
            for(j=0; j<=i; j++) // inner loop for rows
            { 
                // printing num with a space  
                System.out.print(num+ " "); 
   
                //incrementing value of num 
                num++; 
            } 
   
            // ending line after each row 
            System.out.println(); 
        } 
    } 
       public static void main(String args[]) 
    { 
        int n = 5; 
        printNums(n); 
    } 
}

2. Number Pattern Program in java

1 
2 3 
4 5 6 
7 8 9 10 
11 12 13 14 15 

 

import java.util.Scanner;
 
public class Edureka
{            
        public static void main(String[] args) {
            int i, j, k = 1;
            for (i = 1; i <= 5; i++) {
                for (j = 1; j< i + 1; j++) {
                    System.out.print(k++ + " ");
                }
      
                System.out.println();
            }
        }
      
    }

 

3. Pascal’s Triangle Program in Java

 

             1
           1   1
         1   2   1
       1   3   3   1
     1   4   6   4   1

 


import java.util.Scanner;

public class Edureka
{			 
	public static void main(String[] args) {
		 
        int n = 5;
 
        for (int i = 0; i < n; i++) {
            int number = 1;
            System.out.printf("%" + (n - i) * 2 + "s", "");
            for (int j = 0; j <= i; j++) {
                System.out.printf("%4d", number);
                number = number * (i - j) / (j + 1);
            }
            System.out.println();
        }
 
    }
 
}

 

4. Diamond Pattern Program in Java

 

   1
  212
 32123
4321234
 32123
  212
   1

 

import java.util.Scanner;
 
public class Edureka
{            
    public static void main(String[] args) {
        for (int i = 1; i <= 4; i++)
        {
            int n = 4;
  
            for (int j = 1; j<= n - i; j++) { System.out.print(" "); } for (int k = i; k >= 1; k--)
            {
                System.out.print(k);
            }
            for (int l = 2; l <= i; l++) { System.out.print(l); } System.out.println(); } for (int i = 3; i >= 1; i--)
        {
            int n = 3;
  
            for (int j = 0; j<= n - i; j++) { System.out.print(" "); } for (int k = i; k >= 1; k--)
            {
                System.out.print(k);
            }
            for (int l = 2; l <= i; l++)
            {
                System.out.print(l);
            }
  
            System.out.println();
        }
      
    }
}

 

5. Number Pattern Program in Java

Enter the number of rows: 5

1 
2 2 
3 3 3 
4 4 4 4 
5 5 5 5 5

 


import java.util.Scanner;
 
public class Edureka
{            
    public static void main(String[] args) 
    {
        Scanner sc = new Scanner(System.in); //Taking rows value from the user    
        System.out.println("Enter the number of rows: ");    
        int rows = sc.nextInt();         
        for (int i = 1; i <= rows; i++) 
        {
            for (int j = 1; j <= i; j++)
            {
                System.out.print(i+" ");
            }
              
            System.out.println();
        }         
        sc.close();
    }
}

 

6. Descending order Pattern

Enter the number of rows: 5

5 
5 4 
5 4 3 
5 4 3 2 
5 4 3 2 1

 

import java.util.Scanner;
public class Edureka
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);

//Taking rows value from the user

System.out.println("Enter the number of rows: ");

int rows = sc.nextInt();
for (int i = rows; i >= 1; i--)
{
for (int j = rows; j >= i; j--)
{
System.out.print(j+" ");
}

System.out.println();
}
sc.close();
}
}

 

7. Right Triangle Numeric Pattern 

Enter the number of rows: 5

1 
2 1 
3 2 1 
4 3 2 1 
5 4 3 2 1

 


import java.util.Scanner;
public class Edureka
{
	 
public static void main(String[] args) 
{
Scanner sc = new Scanner(System.in);
     
System.out.println("Enter the number of rows: ");

int rows = sc.nextInt();
         
for (int i = 1; i <= rows; i++) { for (int j = i; j >= 1; j--)
   {
       System.out.print(j+" ");
   }
    
   System.out.println();
}         
sc.close();
}
}

 

8. Binary Number Pattern

Enter the number of rows: 5

10101
01010
10101
01010
10101

 


import java.util.Scanner;
public class Edureka
{
	 
	public static void main(String[] args) 
    {
        Scanner sc = new Scanner(System.in);
         
        System.out.println("Enter the number of rows: ");
         
        int rows = sc.nextInt();
         
        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();
        }
         
        sc.close();
    }
}			 
	

 

9. Zeros/ ones Pattern Programs

Enter the number of rows: 5

1
10
101
1010
10101

 


import java.util.Scanner;

public class Edureka
{			 
	public static void main(String[] args) 
    {
        Scanner sc = new Scanner(System.in);
         
        System.out.println("Enter the number of rows: ");
         
        int rows = sc.nextInt();         
        for (int i = 1; i <= rows; i++) 
        {
            for (int j = 1; j <= i; j++)
            {
                if(j%2 == 0)
                {
                    System.out.print(0);
                }
                else
                {
                    System.out.print(1);
                }
            }
             
            System.out.println();
        }
         
        sc.close();
    }
}

 

10. Diamond Numeric Pattern

 

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
import java.util.Scanner;
public class Edureka
{
	public static void main(String[] args) 
    {
         
        int n = 5;  
         
        for (int i = 1; i <= n; i++) 
        {
            for (int j = 1; j < i; j++) 
            {
                System.out.print(" ");
            }
             
            for (int k = i; k <= n; k++) { System.out.print(k+" "); } System.out.println(); } for (int i = n-1; i >= 1; i--) 
        {
             for (int j = 1; j < i; j++) 
            {
                System.out.print(" ");
            }
            for (int k = i; k <= n; k++)
            {
                System.out.print(k+" ");
            }
             
            System.out.println();
        }
        
    }
}


Now that we have implemented numeric pattern programs in Java. Let us move further and implement some character/ alphabet patterns.

Alphabet/ Character Patterns in Java

 

1. Right Alphabetic triangle 

A 
A B 
A B C 
A B C D 
A B C D E 
A B C D E F

 


import java.util.Scanner;

public class Edureka
{			 
	public static void main(String[] args)
    {
        int alphabet = 65;
                for (int i = 0; i <= 5; i++)
        {
            for (int j = 0; j <= i; j++)
            {
                System.out.print((char) (alphabet + j) + " ");
            }
            System.out.println();
        }
    }
}
   

 

2. Alphabet/ Character Pattern Programs

A 
B B 
C C C 
D D D D 
E E E E E 
F F F F F F 

 


import java.util.Scanner;

public class Edureka
{			 
	public static void main(String[] args)
    {
        int alphabet = 65;
                for (int i = 0; i<= 5; i++)
        {
            for (int j = 0; j <= i; j++)
            {
                System.out.print((char) alphabet + " ");
            }
            alphabet++;
            System.out.println();
        }
    }
}

   

 

3. K Shape Character Pattern Program

A B C D E F 
A B C D E 
A B C D 
A B C 
A B 
A 
A 
A B 
A B C 
A B C D 
A B C D E 
A B C D E F

 


import java.util.Scanner;

public class Edureka
{public static void main(String[] args)
{
for (int i = 5; i >= 0; i--)
{
   int alphabet = 65;
   for (int j = 0; j <= i; j++)
   {
       System.out.print((char) (alphabet + j) + " ");
   }
   System.out.println();
}
for (int i = 0; i<= 5; i++)
{
   int alphabet = 65;
   for (int j = 0; j <= i; j++)
   {
       System.out.print((char) (alphabet + j) + " ");
   }
   System.out.println();
}
}
}

   

4. Triangle Character Pattern Program in Java

     A 
    A B 
   A B C 
  A B C D 
 A B C D E 
A B C D E F

 

 


public class Edureka
{			 
	public static void main(String[] args)
{
		for (int i = 0; i <= 5; i++) { int alphabet = 65; for (int j = 5; j > i; j--)
        {
            System.out.print(" ");
        }
        for (int k = 0; k <= i; k++)
        {
            System.out.print((char) (alphabet + k) + " ");
        }
        System.out.println();
    }
}
}

 

5. Diamond Pattern in Java

Enter a Character between A to Z : F

     A
    B B
   C   C
  D     D
 E       E
F         F
 E       E
  D     D
   C   C
    B B
     A

 

import java.util.Scanner;

public class Edureka
{public static void main(String[] args) {
    char[] letter = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
            'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
            'W', 'X', 'Y', 'Z' };
    int letter_number = 0;
    String[] diamond = new String[26]; // array of strings
    System.out.print("Enter a Character between A to Z : ");

    Scanner reader = new Scanner(System.in);
    try {
        char user_letter = reader.next("[A-Z]").charAt(0);
        // search for letter number in the array letter
        for (int i = 0; i < letter.length; i++) {
            if (letter[i] == user_letter) {
                letter_number = i;
                break;
            }
        }

        // construct diamond
        for (int i = 0; i <= letter_number; i++) {
            diamond[i] = "";
            // add initial spaces
            for (int j = 0; j < letter_number - i; j++) {
                diamond[i] += " ";
            }

            // add letter
            diamond[i] += letter[i];

            // add space between letters
            if (letter[i] != 'A') {
                for (int j = 0; j < 2 * i - 1; j++) { diamond[i] += " "; } // add letter diamond[i] += letter[i]; } // Draw the first part of the diamond System.out.println(diamond[i]); } for (int i = letter_number - 1; i >= 0; i--)
				{
            // Draw the second part of the diamond
            // Writing the diamondArray in reverse order
            System.out.println(diamond[i]);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        reader.close();
    }

}
}

So this brings us to the end of the top 30 pattern programs in the Java. I hope you found it informative and helped you in understanding Java Fundamentals

Check out the Java Certification Course by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. We are here to help you with every step on your journey, for becoming a besides this java interview questions, we come up with a curriculum which is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

Got a question for us? Please mention it in the comments section and we will get back to you as soon as possible or you can also join our Java Training in Amravati.

Upcoming Batches For Java Certification Training Course
Course NameDateDetails
Java Certification Training Course

Class Starts on 4th May,2024

4th May

SAT&SUN (Weekend Batch)
View Details
Java Certification Training Course

Class Starts on 25th May,2024

25th May

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

Top 30 Pattern Program in Java: How to Print Star, Number and Character

edureka.co