Home >>Java Tutorial >Java Star Pattern

Print star pattern in Java

Print star pattern in Java

In this following tutorial of print pattern in Java, the programmers who are very much keen about learning Java programming will do a little fun while coding the program. These patterns can be of many types and here we are going to learn about how to print star pattern in Java.

This tutorial will guide you in learning about the process of pattern printing in Java. These are basically the questions that are mostly asked in the interviews to check the understanding of the interviewee and should be learned by everyone.

These questions are generally solved by the programmers that possess a great understanding of the nested loops. This concept of star pattern programs in Java can also be used to solve various problems in C/C++/PHP and any other programming languages, the only difference will be of the syntax of the codes.

This tutorial will take you to a journey of learning about the pattern programs in Java and all the different ways and methods including the various patterns that are involved in this topic.

Here is the entire important star pattern that are used or printed by the programmers in the Java programming language that are depicted below:

Pattern 1

public class Pattern
{ 
    public static void main(String args[])
    { 
        for (int i=1; i<=5; i++) 
        { 
            for (int j=1; j<=i; j++ )
            { 
                System.out.print(j);
            } 
  
            System.out.println();
        }  
    } 

}
Output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

Pattern 2

public class Pattern
{ 
    public static void main(String args[])
    { 
        for (int i=1; i<=5; i++) 
        { 
            for (int j=1; j<=i; j++ )
            { 
                System.out.print(i);
            } 
  
            System.out.println();
        }  
    } 

}
Output:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

Pattern 3

public class Pattern
{ 
    public static void main(String args[])
    { 
        for (int i=1; i<=5; i++) 
        { 
            for (int j=1; j<=i; j++ )
            { 
                System.out.print("* ");
            } 
  
            System.out.println();
        }  
    } 

}
Output:
*
* *
* * *
* * * *
* * * * *

Pattern 4

public class Pattern
{ 
    public static void main(String args[])
    { 
        for (int i=1; i<=5; i++) 
        { 
            for (int k=5; k>i; k-- )
            { 
                System.out.print(" ");
            } 
            for (int j=1; j<=i; j++ )
            { 
                System.out.print("* ");
            } 
  
            System.out.println();
        }  
    } 

}
Output:
    *
   * *
  * * *
 * * * *
* * * * *

Pattern 5

public class Pattern
{ 
    public static void main(String args[])
    { 
        for (int i=1; i<=5; i++) 
        { 
            for (int j=5; j>=i; j-- )
            { 
                System.out.print("* ");
            } 
  
            System.out.println();
        }  
    } 

}
Output:
* * * * *
* * * *
* * *
* *
*

Pattern 6

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

}
Output:
     *
   * *
  * * *
 * * * *
* * * * *
 * * * *
  * * *
   * *
     *

Pattern 7

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

}
Output:
*
* *
* * *
* * * *
* * * * *
* * * * *
* * * *
* * *
* *
*

Pattern 8

public class Pattern
{ 
    public static void main(String args[])
    { 
        for (int i=5; i>=1; i--) 
        {  
            if (i%2 != 0) 
            {
                for (int j=5; j>=i; j-- )
                { 
                    System.out.print(" * ");
                } 
  
            System.out.println();
            }
        }  
        for(int i=2; i<=5; i++)
        {  
            if(i%2 !=0)
            {
                for(int j=5; j>=i; j--)
                {  
                    System.out.print(" * ");  
                }  
            
            System.out.println(); 
            }
        }  
    } 

}
Output:
*
* * *
* * * * *
* * *
*

Pattern 9

public class Pattern
{ 
    public static void main(String args[])
    { 
        for (int i=1; i<=5; i++) 
        {  
            for (int j=1; j<=5; j++)
            { 
                 System.out.print(i*j+" ");
            } 
  
            System.out.println();
        }  
    } 

}
Output:
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25

Pattern 10

public class Pattern
{ 
    public static void main(String args[])
    { 
        int x = 1;
        
        for (int i=1; i<=5; i++) 
        {  
            for (int j=1; j<=5; j++)
            { 
                 System.out.print(x++);
            } 
  
            System.out.println();
        }  
    } 

}
Output:
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25

Pattern 11

public class Pattern
{ 
    public static void main(String args[])
    { 
        int x = 0;
        int j = 1;
        
        for (int i=1; i<=3; i++) 
        {  
            while(x < 3) 
		    {
			System.out.print(j++);
			x++;
		    }
		    x = 0;
            System.out.println();
        }  
    } 

}
Output:
1 2 3
4 5 6
7 8 9