Java Patterns - Level 3

15+ Java Pattern Questions With Solutions

Java Patterns - Level 3

Pattern Programs in Java Based on:

  • Numeric Patterns

  • Character Patterns

  • Star Patterns in Java

  • Mixed patterns

Patter Program - level 3

Pattern Program 48::

Pyramid Program

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

Pattern Program 49::

Right Triangle Star Pattern

*
* *
* * *
* * * *
* * * * *
public class Pattern
{ 
    public static void main(String args[]) 
    { 
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the value of n: ");
        int n = sc.nextInt(); 
        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
        } 
    } 
}

Pattern Program 50::

Left Triangle Star Pattern

           * 
         * * 
       * * * 
     * * * * 
   * * * * *
public class Pattern 
{ 
    public static void main(String args[]) 
    { 
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the value of n: ");
        int n = sc.nextInt(); 
        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
        } 
    } 
}

Pattern Program 51::

Diamond Shape Pattern Program in Java

Enter the number of rows: 5

    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *
import java.util.Scanner;
public class Pattern
{
    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("");
        }
    }
}

Pattern Program 52::

Downward Triangle Star Pattern

Enter the number of rows: 5

* * * * * 
* * * * 
* * * 
* * 
*
import java.util.Scanner;
public class Pattern
{
    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-1; i>=0 ; i--)
    {
        for (int j=0; j<=i; j++)
        {
            System.out.print("*" + " ");
        }
    System.out.println();
    }
    sc.close();
   }
}

Pattern Program 53::

Mirrored Right Triangle Star Program

Enter the number of rows: 5

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

Pattern Program 54::

Reversed Pyramid Star Pattern

Enter the number of rows: 5

* * * * * 
 * * * * 
  * * * 
   * * 
    *
import java.util.Scanner;
public class Pattern
{
    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();

  }
}

Pattern Program 55::

Right Pascal’s Triangle

Enter the number of rows: 5

* 
* * 
* * * 
* * * * 
* * * * * 
* * * * 
* * * 
* * 
*
import java.util.Scanner;
public class Pattern
{
    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();
    }
}

Pattern Program 56::

Left Triangle Pascal’s

Enter the number of rows: 5

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

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

Pattern Program 57::

Sandglass Star Pattern

Enter the number of rows: 5

* * * * * 
 * * * * 
  * * * 
   * * 
    * 
    * 
   * * 
  * * * 
 * * * * 
* * * * *
import java.util.Scanner;
public class Pattern
{
    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();
    }
}

Pattern Program 58::

Triangle Star pattern

Enter the number of rows: 5

    *
   * *
  *   *
 *     *
*********
import java.util.Scanner;
public class Pattern
{
     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();
        }
    }

Pattern Program 59::

Down triangle

Enter the number of rows: 5

*********
 *     *
  *   *
   * *
    *
import java.util.Scanner;
public class Pattern
{
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();
  }
}

Pattern Program 60::

Diamond Star Pattern

Enter the number of rows: 5

    *
   * *
  *   *
 *     *
*       *
 *     *
  *   *
   * *
    *
import java.util.Scanner;
public class Pattern
{
    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();
}
}

Pattern Program 61::

Number Pattern Program

1 
2 3 
4 5 6 
7 8 9 10 
11 12 13 14 15
import java.util.Scanner;

public class Pattern
{            
        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();
            }
        }

    }

Pattern Program 62::

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

    }

}

Pattern Program 63::

Diamond Pattern Program in Java

   1
  212
 32123
4321234
 32123
  212
   1
import java.util.Scanner;

public class Pattern
{            
    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();
        }

    }
}

Pattern Program 64::

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

    }
}