Java Program to print star pattern-pyramid



Welcome,

Let's learn how to write a Java code for star patter (Pyramid) by taking input from user(using scanner).

First let's do this..

a) Pattern 1

Java Pyramid pattern
Pattern 1
         


This is a simple pyramid pattern and you can understand very easily.

import java.util.*;

public class Pattern1 {

    public static void main(String args[]){

        Scanner sc=new Scanner(System.in);

        int n=sc.nextInt();

        for(int i=0;i<n;i++){

            for(int st=0;st<=i;st++){

                System.out.print("*");

            }

            System.out.println();

        }

    }

}


b) pattern 2

inverted pyramid pattern java
Pattern 2
        
It's very simple once you understand it.

import java.util.*;

public class Pattern2 {

    public static void main(String args[]){

        Scanner sc=new Scanner(System.in);

        int n=sc.nextInt();

        for(int i=n;i>0;i--){

            for(int st=i;st>0;st--){               

System.out.print(" * ");

            }           

System.out.println();

        }

    }  

}


Thank you🙏

Comments