Butterfly Pattern Program in Java Language

 

class Demo
{
    public static void main(String args[])
    {
        int n=5;
        //First half-- Upper
        for(int i=1; i<=n; i++){
            //Upper part
            for(int j=1; j<=i; j++){
                System.out.print("*");
            }
            //spaces
            int spaces = 2*(n-i);
            for(int j=1; j<=spaces; j++){
                System.out.print(" ");
            }

            //2nd part
             for(int j=1; j<=i; j++){
                System.out.print("*");
            }
            System.out.println();
        }

        //LowerHalf
       for(int i=n; i>=1; i--){
            //Upper part
            for(int j=1; j<=i; j++){
                System.out.print("*");
            }
            //spaces
            int spaces = 2*(n-i);
            for(int j=1; j<=spaces; j++){
                System.out.print(" ");
            }

            //2nd part
             for(int j=1; j<=i; j++){
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

OUTPUT:
* * ** ** *** *** **** **** ********** ********** **** **** *** *** ** ** * *

Comments

Popular posts from this blog

Write A C Program To Delete A Single Element From An Array

Evolution of Computer Devices