Monday, November 18, 2024
Google search engine
HomeLanguagesJavaJava Pattern Programs

Java Pattern Programs

Star, number, and character patterns are the most asked Java Pattern Programs in interviews to check your logical and coding skills. Pattern programs in Java help you to sharpen your looping concepts(for loop). To learn pattern programs, you must have knowledge of Java Loops.

Patterns Programs in Java

Patterns Programs in Java

Java Pattern Programs

Here, you will find the top 25 Java pattern programs with their proper code and explanation. 

  1. Square Hollow Pattern
  2. Number triangle Pattern
  3. Number-increasing Pyramid Pattern
  4. Number-increasing reverse Pyramid Pattern
  5. Number-changing Pyramid Pattern
  6. Zero-One triangle Pattern
  7. Palindrome triangle Pattern
  8. Rhombus Pattern
  9. Diamond Star Pattern
  10. Butterfly star Pattern
  11. Square fill Pattern
  12. Right Half Pyramid Pattern
  13. Reverse Right Half Pyramid Pattern
  14. Left Half Pyramid Pattern
  15. Reverse Left Half Pyramid Pattern
  16. Triangle star Pattern
  17. Reverse number triangle Pattern
  18. Mirror Image triangle Pattern
  19. Hollow triangle Pattern
  20. Hollow reverse triangle Pattern
  21. Hollow Diamond Pyramid
  22. Hollow Hourglass Pattern
  23. Pascal’s Triangle
  24. Right Pascal’s Triangle
  25. K Pattern

1. Square Hollow Pattern

Java




// Java Program to print pattern
// Square hollow pattern
import java.util.*;
  
public class GeeksForGeeks {
    // Function to demonstrate pattern
    public static void printPattern(int n)
    {
        int i, j;
        // outer loop to handle number of rows
        for (i = 0; i < n; i++) {
            //  inner loop to handle number of columns
            for (j = 0; j < n; j++) {
                // star will print only when  it is in first
                // row or last row or first column or last
                // column
                if (i == 0 || j == 0 || i == n - 1
                    || j == n - 1) {
                    System.out.print("*");
                }
                // otherwise print space only.
                else {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
    }
  
    // Driver Function
    public static void main(String args[])
    {
        int n = 6;
        printPattern(n);
    }
}


C#




// C# Program to print pattern Square hollow pattern
  
using System;
  
public class GFG{
    
      // Function to demonstrate pattern
    public static void printPattern(int n)
    {
        int i, j;
        // outer loop to handle number of rows
        for (i = 0; i < n; i++) {
            //  inner loop to handle number of columns
            for (j = 0; j < n; j++) {
                // star will print only when  it is in first
                // row or last row or first column or last
                // column
                if (i == 0 || j == 0 || i == n - 1
                    || j == n - 1) {
                    Console.Write("*");
                }
                // otherwise print space only.
                else {
                    Console.Write(" ");
                }
            }
            Console.WriteLine();
        }
    }
  
    static public void Main (){
  
        // Code
          int n = 6;
        printPattern(n);
    }
}
  
// This code is contributed by sankar.


Output

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

2. Number triangle Pattern

Java




// Java Program to print pattern
// Number triangle pattern
import java.util.*;
  
public class GeeksForGeeks {
    // Function to demonstrate pattern
    public static void printPattern(int n)
    {
        int i, j;
        // outer loop to handle number of rows
        for (i = 1; i <= n; i++) {
            // inner loop to print space
            for (j = 1; j <= n - i; j++) {
                System.out.print(" ");
            }
            // inner loop to print star
            for (j = 1; j <= i; j++) {
                System.out.print(i + " ");
            }
            // print new line for each row
            System.out.println();
        }
    }
  
    // Driver Function
    public static void main(String args[])
    {
        int n = 6;
        printPattern(n);
    }
}


C#




// C# Program to print pattern Number triangle pattern
  
using System;
  
public class GFG {
  
    // Function to demonstrate pattern
    static void printPattern(int n)
    {
        int i, j;
        // outer loop to handle number of rows
        for (i = 1; i <= n; i++) {
            // inner loop to print space
            for (j = 1; j <= n - i; j++) {
                Console.Write(" ");
            }
            // inner loop to print star
            for (j = 1; j <= i; j++) {
                Console.Write(i + " ");
            }
            // print new line for each row
            Console.WriteLine();
        }
    }
  
    static public void Main()
    {
  
        // Code
        int n = 6;
        printPattern(n);
    }
}
  
// this code is contributed by lokesh


Output

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

3. Number-increasing Pyramid Pattern 

Java




// Java Program to print pattern
// Number-increasing pyramid 
import java.util.*;
  
public class GeeksForGeeks {
    // Function to demonstrate pattern
    public static void printPattern(int n)
    {
        int i, j;
        // outer loop to handle number of rows
        for (i = 1; i <= n; i++) {
            // inner loop to handle number of columns
            for (j = 1; j <= i; j++) {
                // printing column values upto the row
                // value.
                System.out.print(j + " ");
            }
  
            // print new line for each row
            System.out.println();
        }
    }
  
    // Driver Function
    public static void main(String args[])
    {
        int n = 6;
        printPattern(n);
    }
}


Output

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

4. Number-increasing reverse Pyramid Pattern

Java




// Java Program to print pattern
// Number-increasing reverse pyramid
import java.util.*;
  
public class GeeksForGeeks {
    // Function to demonstrate pattern
    public static void printPattern(int n)
    {
        int i, j;
        // outer loop to handle number of rows
        for (i = n; i >= 1; i--) {
            // inner loop to handle number of columns
            for (j = 1; j <= i; j++) {
                // printing column values upto the row
                // value.
                System.out.print(j + " ");
            }
  
            // print new line for each row
            System.out.println();
        }
    }
  
    // Driver Function
    public static void main(String args[])
    {
        int n = 6;
        printPattern(n);
    }
}


Output

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

5. Number-changing Pyramid Pattern

Java




// Java Program to print pattern
// Number-changing pyramid
import java.util.*;
  
// Java code for printing pattern
public class GeeksForGeeks {
    // Function to demonstrate pattern
    public static void printPattern(int n)
    {
        int i, j;
        int num = 1;
        // outer loop to handle number of rows
        for (i = 1; i <= n; i++) {
            // inner loop to handle number of columns
            for (j = 1; j <= i; j++) {
                // printing value of num in each iteration.
                System.out.print(num + " ");
                // increasing the value of num.
                num++;
            }
  
            // printing new line for each row
            System.out.println();
        }
    }
  
    // Driver Function
    public static void main(String args[])
    {
        int n = 6;
        printPattern(n);
    }
}


Output

1 
2 3 
4 5 6 
7 8 9 10 
11 12 13 14 15 
16 17 18 19 20 21 

6. Zero-One Triangle Pattern

Java




// Java Program to print pattern
// Zero-One triangle
import java.util.*;
  
public class GeeksForGeeks {
    // Function to demonstrate pattern
    public static void printPattern(int n)
    {
        int i, j;
        //outer loop to handle number of rows
        for (i = 1; i <= n; i++) {
            //inner loop to handle number of columns
            for (j = 1; j <= i; j++) {
                // if the sum of (i+j) is even then print 1
                if ((i + j) % 2 == 0) {
                    System.out.print(1 + " ");
                }
                // otherwise print 0
                else {
                    System.out.print(0 + " ");
                }
            }
  
            //printing new line for each row
            System.out.println();
        }
    }
  
    // Driver Function
    public static void main(String args[])
    {
        int n = 6;
        printPattern(n);
    }
}


Output

1 
0 1 
1 0 1 
0 1 0 1 
1 0 1 0 1 
0 1 0 1 0 1 

7. Palindrome Triangle Pattern

Java




// Java Program to print pattern
// Palindrome triangle
import java.util.*;
  
public class GeeksForGeeks {
    // Function to demonstrate pattern
    public static void printPattern(int n)
    {
        int i, j;
  
        // outer loop to handle number of rows
        for (i = 1; i <= n; i++) {
            // inner loop to print the spaces
            for (j = 1; j <= 2 * (n - i); j++) {
                System.out.print(" ");
            }
  
            // inner loop to print the first part
            for (j = i; j >= 1; j--) {
                System.out.print(j + " ");
            }
  
            // inner loop to print the second part
            for (j = 2; j <= i; j++) {
                System.out.print(j + " ");
            }
  
            // printing new line for each row
            System.out.println();
        }
    }
  
    // Driver Function
    public static void main(String args[])
    {
        int n = 6;
        printPattern(n);
    }
}


Output

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

8. Rhombus Pattern

Java




// Java Program to print
// Rhombus pattern
import java.util.*;
  
public class GeeksForGeeks {
    // Function to demonstrate pattern
    public static void printPattern(int n)
    {
        int i, j;
        int num = 1;
        // outer loop to handle number of rows
        for (i = 1; i <= n; i++) {
            // inner loop to print spaces
            for (j = 1; j <= n - i; j++) {
                System.out.print(" ");
            }
  
            // inner loop to print stars
            for (j = 1; j <= n; j++) {
                System.out.print("*");
            }
            // printing new line for each row
            System.out.println();
        }
    }
  
    // Driver Function
    public static void main(String args[])
    {
        int n = 6;
        printPattern(n);
    }
}


Output

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

9. Diamond Star Pattern

Java




// Java Program to print
// Diamond Star Pattern
import java.util.*;
  
public class GeeksForGeeks {
  
    // Function to demonstrate pattern
    public static void printPattern(int n)
    {
        int i, j;
        int num = 1;
        // outer loop to handle upper part
        for (i = 1; i <= n; i++) {
            // inner loop to print spaces
            for (j = 1; j <= n - i; j++) {
                System.out.print(" ");
            }
            // inner loop to print stars
            for (j = 1; j <= 2 * i - 1; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
  
        // outer loop to handle lower part
        for (i = n-1; i >= 1; i--) {
            // inner loop to print spaces
            for (j = 1; j <= n - i; j++) {
                System.out.print(" ");
            }
            // inner loop to print stars
            for (j = 1; j <= 2 * i - 1; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
  
    // Driver Function
    public static void main(String args[])
    {
        int n = 6;
        printPattern(n);
    }
}


Output

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

10. Butterfly Star Pattern

Java




// Java Program to print
// Butterfly Pattern
import java.util.*;
  
// Java code for printing pattern
public class GeeksForGeeks {
  
    // Function to demonstrate pattern
    public static void printPattern(int n)
    {
        int i, j;
        int num = 1;
  
        // outer loop to handle upper part
        for (i = 1; i <= n; i++) {
            // inner loop to print stars
            for (j = 1; j <= i; j++) {
                System.out.print("*");
            }
  
            // inner loop to print spaces
            int spaces = 2 * (n - i);
            for (j = 1; j <= spaces; j++) {
                System.out.print(" ");
            }
  
            // inner loop to print stars
            for (j = 1; j <= i; j++) {
                System.out.print("*");
            }
  
            System.out.println();
        }
  
        // outer loop to handle lower part
        for (i = n; i >= 1; i--) {
            // inner loop to print stars
            for (j = 1; j <= i; j++) {
                System.out.print("*");
            }
  
            // inner loop to print spaces
            int spaces = 2 * (n - i);
            for (j = 1; j <= spaces; j++) {
                System.out.print(" ");
            }
  
            // inner loop to print stars
            for (j = 1; j <= i; j++) {
                System.out.print("*");
            }
  
            System.out.println();
        }
    }
  
    // Driver Function
    public static void main(String args[])
    {
        int n = 6;
        printPattern(n);
    }
}


Output

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

11. Square Fill Pattern

Java




// Java Program to print
// Square fill pattern
import java.util.*;
  
public class GeeksForGeeks {
  
    // Function to demonstrate pattern
    public static void printPattern(int n)
    {
        int i, j;
  
        // outer loop to handle rows
        for (i = 0; i <= n; i++) {
  
            // inner loop to handle columns
            for (j = 0; j <= n; j++) {
                System.out.print("*");
            }
  
            // printing new line for each row
            System.out.println();
        }
    }
  
    // Driver Function
    public static void main(String args[])
    {
        int n = 6;
        printPattern(n);
    }
}


Output

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

12. Right Half Pyramid Pattern

Java




// Java Program to print
// Pyramid pattern
import java.util.*;
  
public class GeeksForGeeks {
    // Function to demonstrate pattern
    public static void printPattern(int n)
    {
        int i, j;
  
        // outer loop to handle rows
        for (i = 1; i <= n; i++) {
  
            // inner loop to handle columns
            for (j = 1; j <= i; j++) {
                System.out.print("*");
            }
  
            // printing new line for each row
            System.out.println();
        }
    }
  
    // Driver Function
    public static void main(String args[])
    {
        int n = 6;
        printPattern(n);
    }
}


Output

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

13. Reverse Right Half Pyramid Pattern

Java




// Java Program to print pattern
// Reverse Right Half Pyramid
import java.util.*;
  
public class GeeksForGeeks {
    // Function to demonstrate pattern
    public static void printPattern(int n)
    {
        int i, j;
  
        // outer loop to handle rows
        for (i = n; i >= 1; i--) {
  
            // inner loop to handle columns
            for (j = 1; j <= i; j++) {
                System.out.print("*");
            }
  
            // printing new line for each row
            System.out.println();
        }
    }
  
    // Driver Function
    public static void main(String args[])
    {
        int n = 6;
        printPattern(n);
    }
}


Output

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

14. Left Half Pyramid Pattern

Java




// Java Program to print pattern
// Left Half Pyramid pattern
import java.util.*;
  
public class GeeksForGeeks {
    // Function to demonstrate pattern
    public static void printPattern(int n)
    {
        int i, j;
  
        // outer loop to handle rows
        for (i = n; i >= 1; i--) {
  
            // inner loop to print spaces.
            for (j = 1; j < i; j++) {
                System.out.print(" ");
            }
  
            // inner loop to print stars.
            for (j = 0; j <= n - i; j++) {
                System.out.print("*");
            }
  
            // printing new line for each row
            System.out.println();
        }
    }
  
    // Driver Function
    public static void main(String args[])
    {
        int n = 6;
        printPattern(n);
    }
}


Output

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

15. Reverse Left Half Pyramid Pattern

Java




// Java Program to print pattern
// Reverse Left Half Pyramid 
import java.util.*;
  
public class GeeksForGeeks {
    // Function to demonstrate pattern
    public static void printPattern(int n)
    {
        int i, j;
  
        // calculating number of spaces
        int num = 2 * n - 2;
  
        // outer loop to handle rows
        for (i = n; i > 0; i--) {
            // inner loop to print spaces.
            for (j = 0; j < n - i; j++) {
                System.out.print(" ");
            }
            // Decrementing value of num after each loop
            num = num - 2;
            // inner loop to print stars.
            for (j = 0; j < i; j++) {
                System.out.print("*");
            }
  
            // printing new line for each row
            System.out.println();
        }
    }
  
    // Driver Function
    public static void main(String args[])
    {
        int n = 6;
        printPattern(n);
    }
}


Output

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

16. Triangle Star Pattern

Java




// Java Program to print
// Triangular Pattern
import java.util.*;
  
public class GeeksForGeeks {
    // Function to demonstrate pattern
    public static void printPattern(int n)
    {
        int i, j;
        // outer loop to handle rows
        for (i = 0; i < n; i++) {
            // inner loop to print spaces.
            for (j = n - i; j > 1; j--) {
                System.out.print(" ");
            }
  
            // inner loop to print stars.
            for (j = 0; j <= i; j++) {
                System.out.print("* ");
            }
  
            // printing new line for each row
            System.out.println();
        }
    }
  
    // Driver Function
    public static void main(String args[])
    {
        int n = 6;
        printPattern(n);
    }
}


Output

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

17. Reverse number Triangle Pattern

Java




// Java Program to print pattern
// Reverse number triangle
import java.util.*;
  
public class GeeksForGeeks {
  
    // Function to demonstrate pattern
    public static void printPattern(int n)
    {
        int i, j;
        // outer loop to handle rows
        for (i = 1; i <= n; i++) {
  
            // inner loop to print spaces.
            for (j = 1; j < i; j++) {
                System.out.print(" ");
            }
  
            // inner loop to print value of j.
            for (j = i; j <= n; j++) {
                System.out.print(j + " ");
            }
  
            // printing new line for each row
            System.out.println();
        }
    }
  
    // Driver Function
    public static void main(String args[])
    {
        int n = 6;
        printPattern(n);
    }
}


Output

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

18. Mirror Image Triangle Pattern

Java




// Java Program to print pattern
// Mirror Image of a triangle
import java.util.*;
  
public class GeeksForGeeks {
    // Function to demonstrate pattern
    public static void printPattern(int n)
    {
        int i, j;
        // Printing the upper part
        for (i = 1; i <= n; i++) {
            // inner loop to print spaces.
            for (j = 1; j < i; j++) {
                System.out.print(" ");
            }
            // inner loop to print value of j.
            for (j = i; j <= n; j++) {
                System.out.print(j + " ");
            }
  
            // printing new line for each row
            System.out.println();
        }
  
        // Printing the lower part
        for (i = n - 1; i >= 1; i--) {
            // inner loop to print spaces.
            for (j = 1; j < i; j++) {
                System.out.print(" ");
            }
            // inner loop to print value of j.
            for (j = i; j <= n; j++) {
                System.out.print(j + " ");
            }
            // printing new line for each row
            System.out.println();
        }
    }
  
    // Driver Function
    public static void main(String args[])
    {
        int n = 6;
        printPattern(n);
    }
}


Output

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

19. Hollow Triangle Pattern

Java




// Java Program to print 
// Hollow triangle pattern
import java.util.*;
  
public class GeeksForGeeks {
    // Function to demonstrate pattern
    public static void printPattern(int n)
    {
        int i, j, k;
  
        // outer loop to handle rows
        for (i = 1; i <= n; i++) {
            
            // inner loop to print spaces.
            for (j = i; j < n; j++) {
                System.out.print(" ");
            }
            
            for (k = 1; k <= (2 * i - 1); k++) {
                // printing stars.
                if (k == 1 || i == n || k == (2 * i - 1)) {
                    System.out.print("*");
                }
                // printing spaces.
                else {
                    System.out.print(" ");
                }
            }
  
            System.out.println("");
        }
    }
  
    // Driver Function
    public static void main(String args[])
    {
        int n = 6;
        printPattern(n);
    }
}


Output

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

20. Hollow Reverse Triangle Pattern

Java




// Java Program to print pattern
// Reverse Hollow triangle
import java.util.*;
  
public class GeeksForGeeks {
    // Function to demonstrate pattern
    public static void printPattern(int n)
    {
        int i, j, k;
  
        // outer loop to handle rows
        for (i = n; i >= 1; i--) {
  
            // inner loop to print spaces.
            for (j = i; j < n; j++) {
                System.out.print(" ");
            }
  
            for (k = 1; k <= (2 * i - 1); k++) {
                // printing stars.
                if (k == 1 || i == n || k == (2 * i - 1)) {
                    System.out.print("*");
                }
                // printing spaces.
                else {
                    System.out.print(" ");
                }
            }
  
            System.out.println("");
        }
    }
  
    // Driver Function
    public static void main(String args[])
    {
        int n = 6;
        printPattern(n);
    }
}


Output

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

21. Hollow Diamond Pyramid

Java




// Java Program to print Pattern
// Hollow Diamond Star
import java.util.*;
  
public class GeeksForGeeks {
  
    // Function to demonstrate pattern
    public static void printPattern(int n)
    {
        int i, j;
        int num = 1;
        // outer loop to handle upper part
        for (i = 1; i <= n; i++) {
            // inner loop to print spaces
            for (j = 1; j <= n - i; j++) {
                System.out.print(" ");
            }
            // inner loop to print stars
            for (j = 1; j <= 2 * i - 1; j++) {
                if (j == 1 || j == 2*i-1)
                    System.out.print("*");
                else
                    System.out.print(" ");
            }
            System.out.println();
        }
  
        // outer loop to handle lower part
        for (i = n-1; i >= 1; i--) {
            // inner loop to print spaces
            for (j = 1; j <= n - i; j++) {
                System.out.print(" ");
            }
            // inner loop to print stars
            for (j = 1; j <= 2 * i - 1; j++) {
                if (j == 1 || j == 2*i-1)
                    System.out.print("*");
                else
                    System.out.print(" ");
            }
            System.out.println();
        }
    }
    
    // Driver Function
    public static void main(String args[])
    {
        int n = 6;
        printPattern(n);
    }
}


Output

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

22. Hollow Hourglass Pattern

Java




// Java Program to print pattern
// Hollow Hourglass Pattern
import java.util.*;
  
public class GeeksForGeeks {
    
    // Function to demonstrate pattern
    public static void printPattern(int n)
    {
        int i, j;
        
        // Printing the upper part
        for (i = 1; i <= n; i++) {
            // inner loop to print spaces.
            for (j = 1; j < i; j++) {
                System.out.print(" ");
            }
            // inner loop to print value of j.
            for (j = i; j <= n; j++) {
                if(j==i||j==n||i==1)
                    System.out.print("* ");
                else
                    System.out.print("  ");
            }
  
            // printing new line for each row
            System.out.println();
        }
  
        // Printing the lower part
        for (i = n - 1; i >= 1; i--) {
            // inner loop to print spaces.
            for (j = 1; j < i; j++) {
                System.out.print(" ");
            }
            // inner loop to print value of j.
            for (j = i; j <= n; j++) {
                if(j==i||j==n||i==1)
                    System.out.print("* ");
                else
                    System.out.print("  ");
            }
            // printing new line for each row
            System.out.println();
        }
    }
  
    // Driver Function
    public static void main(String args[])
    {
        int n = 6;
        printPattern(n);
    }
}


Output

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

23. Pascal’s Triangle

Java




// Java Program to implement
// Pascal's Triangle
import java.util.*;
  
class GFG {
  
    // Pascal function
    public static void printPascal(int n)
    {
        for (int i = 1; i <= n; i++) {
            for (int j = 0; j <= n - i; j++) {
  
                // for left spacing
                System.out.print(" ");
            }
  
            // used to represent x(i, k)
            int x = 1;
            for (int k = 1; k <= i; k++) {
  
                // The first value in a line is always 1
                System.out.print(x + " ");
                x = x * (i - k) / k;
            }
            System.out.println();
        }
    }
  
    // Driver code
    public static void main(String[] args)
    {
        int n = 4;
        printPascal(n);
    }
}


Output

    1 
   1 1 
  1 2 1 
 1 3 3 1 

24. Right Pascal’s Triangle

Java




// Java Program to print
// Right Pascal’s Triangle
import java.util.*;
  
// Java code for printing pattern
public class Main {
  
    // Function to demonstrate pattern
    public static void printPattern(int n)
    {
        int i, j;
        int num = 1;
  
        // outer loop to handle upper part
        for (i = 1; i <= n; i++) {
            // inner loop to print stars
            for (j = 1; j <= i; j++) {
                System.out.print("* ");
            }
  
            System.out.println();
        }
  
        // outer loop to handle lower part
        for (i = n-1; i >= 1; i--) {
            // inner loop to print stars
            for (j = 1; j <= i; j++) {
                System.out.print("* ");
            }
  
            System.out.println();
        }
    }
  
    // Driver Function
    public static void main(String args[])
    {
        int n = 4;
        printPattern(n);
    }
}


Output

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

25. K Pattern

Java




// Java Program to print pattern
// Reverse Right Half Pyramid
import java.util.*;
  
public class GeeksForGeeks {
    // Function to demonstrate pattern
    public static void printPattern(int n)
    {
        int i, j;
  
        // outer loop to handle rows
        for (i = n; i >= 1; i--) {
  
            // inner loop to handle columns
            for (j = 1; j <= i; j++) {
                System.out.print("*");
            }
  
            // printing new line for each row
            System.out.println();
        }
  
        // outer loop to handle rows
        for (i = 2; i <= n; i++) {
  
            // inner loop to handle columns
            for (j = 1; j <= i; j++) {
                System.out.print("*");
            }
  
            // printing new line for each row
            System.out.println();
        }
    }
  
    // Driver Function
    public static void main(String args[])
    {
        int n = 6;
        printPattern(n);
    }
}


Output

******
*****
****
***
**
*
**
***
****
*****
******
RELATED ARTICLES

Most Popular

Recent Comments