Programs for printing pyramid patterns in Java

By awgladstone on Feb 28, 2019

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

Simple pyramid pattern

filter_none
edit
play_arrow

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

After 180 degree rotation

// Driver Function
public static void main(String args[])
{
int n = 5;
printStars(n);
}
}

// 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 spaces 
            // values changing acc. to requirement 
            for(j=2*(n-i); j>=0; j--) 
            { 
                // printing spaces 
                System.out.print(" "); 
            } 

            //  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[]) 
    { 

Printing Triangle

filter_none
edit
play_arrow

brightness_4
import java.io.*;

// Java code to demonstrate star pattern
public class GeeksForGeeks
{

// Function to demonstrate printing pattern 
    public static void printTriagle(int n) 
    { 
        // 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=n-i; j>1; j--) 
            { 
                // printing spaces 
                System.out.print(" "); 
            } 

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

Comments

Sign in to comment.
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.