Programs for printing pyramid patterns in Java

This article is aimed at giving a Java implementation for pattern printing.

  • Simple pyramid pattern
  • import java.io.*;
    
    // Java code to demonstrate star patterns
    public class GeeksForGeeks
    {
        // Function to demonstrate printing pattern
        public static void printStars(int n)
        {
            int i, j;
    
            // outer loop to handle number of rows
            //  n in this case
            for(i=0; i<n; i++)
            {
    
                //  inner loop to handle number of columns
                //  values changing acc. to outer loop	
                for(j=0; j<=i; j++)
                {
                    // printing stars
                    System.out.print("* ");
                }
    
                // ending line after each row
                System.out.println();
            }
       }
    
        // Driver Function
        public static void main(String args[])
        {
            int n = 5;
            printStars(n);
        }
    }
    

    Output:

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

  • After 180 degree rotation
  • import java.io.*;
    
    // Java code to demonstrate star pattern
    public class GeeksForGeeks
    {
        // Function to demonstrate printing pattern
        public static void printStars(int n)
        {
            // number of spaces
            int i, j, k=2*n-2;
    
            // outer loop to handle number of rows
            //  n in this case
            for(i=0; i<n; i++)
            {
    
                // inner loop to handle number spaces
                // values changing acc. to requirement
                for(j=0; j<k; j++)
                {
                    // printing spaces
                    System.out.print(" ");
                }
                
                // decrementing k after each loop
                k = k - 2;
                
                //  inner loop to handle number of columns
                //  values changing acc. to outer loop
                for(j=0; j<=i; j++)
                {
                    // printing stars
                    System.out.print("* ");
                }
                
                // ending line after each row
                System.out.println();
            }
        }
    
        // Driver Function
        public static void main(String args[])
        {
            int n = 5;
            printStars(n);
        }
    }
    

    Output:

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

  • Printing Triangle
  • import java.io.*;
    
    // Java code to demonstrate star pattern
    public class GeeksForGeeks
    {
        // Function to demonstrate printing pattern
        public static void printTriagle(int n)
        {
            // number of spaces
            int k = 2*n - 2;
     
            // outer loop to handle number of rows
            //  n in this case
            for (int i=0; i<n; i++)
            {
    
                // inner loop to handle number spaces
                // values changing acc. to requirement
                for (int j=0; j<k; j++)
                {
                    // printing spaces
                    System.out.print(" ");
                }
     
                // decrementing k after each loop
                k = k - 1;
     
                //  inner loop to handle number of columns
                //  values changing acc. to outer loop
                for (int j=0; j<=i; j++ )
                {
                    // printing stars
                    System.out.print("* ");
                }
     
                // ending line after each row
                System.out.println();
            }
        }
        
        // Driver Function
        public static void main(String args[])
        {
            int n = 5;
            printTriagle(n);
        }
    }
    

    Output:

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

  • Number Pattern
  • import java.io.*;
    
    // Java code to demonstrate number pattern
    public class GeeksForGeeks
    {
        // Function to demonstrate printing pattern
        public static void printNums(int n)
        {
            int i, j,num;
    
            // outer loop to handle number of rows
            //  n in this case
            for(i=0; i<n; i++)
            {
                // initialising starting number
                num=1;
    
                //  inner loop to handle number of columns
                //  values changing acc. to outer loop
                for(j=0; j<=i; j++)
                {
                    // printing num with a space 
                    System.out.print(num+ " ");
    
                    //incrementing value of num
                    num++;
                }
    
                // ending line after each row
                System.out.println();
            }
        }
    
        // Driver Function
        public static void main(String args[])
        {
            int n = 5;
            printNums(n);
        }
    }
    

    Output:

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

  • Numbers without re assigning
  • import java.io.*;
    
    // Java code to demonstrate star pattern
    public class GeeksForGeeks
    {
        // Function to demonstrate printing pattern
        public static void printNums(int n)
        {
            // initialising starting number
            int i, j, num=1;
            
            // outer loop to handle number of rows
            // n in this case
            for(i=0; i<n; i++)
            {
    
                // without re assigning num
                // num = 1;
                for(j=0; j<=i; j++)
                {
                    // printing num with a space
                    System.out.print(num+ " ");
                    
                    // incrementing num at each column
                    num = num + 1;
                }
    
                // ending line after each row
                System.out.println();
            }
        }
        
        // Driver Function
        public static void main(String args[])
        {
            int n = 5;
            printNums(n);
        }
    }
    

    Output:

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

    This article is contributed by Nikhil Meherwal(S. Shafaq). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

    GATE CS Corner    Company Wise Coding Practice

    Recommended Posts:







    Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.