Java Program to Print Right Pascal and Left Pascal Triangle Face to Face (Butterfly) Star Pattern

Program to Print Butterfly Star Pattern

In this article we are going to see how to print the Reverse k-shape star program.

Example-1

When row value=4
*         *
**      **
***   ***
********
***   ***
**      **
*         *
Example-2:

When row value=5
*            *
**         **
***      ***
****   ****
**********
****   ****
***      ***
**         **
*           *

Now, let’s see program to  how print it.

Explore complete java concepts from the Java programming examples and get ready to become a good programmer and crack the java software developer interview with ease.

Approach:

  • Enter total row and store it in an integer variable row.
  • Take first outer for loop to print all rows of upper part .
    • First inner for loop will print all stars of the column of the left pascal.
    • Second inner loop will print the space of all column value .
    • Third  inner for loop to print all stars of the column values of right pascal .
  • Take first outer for loop to print all rows of lower part .
    • First inner for loop will print all stars of the column of the left pascal.
    • Second inner loop will print the space of all column value .
    • Third  inner for loop to print all stars of the column values of right pascal .

JAVA Code:

Method-1 : Static Star Character

import java.util.*;
public class Main 
{    
    public static void main(String args[])   
    {   
        // taking variable for loop iteration and row .
        int row,r,c,d;
        //creating object 
        Scanner s = new Scanner(System.in);
        // entering the number of row
        System.out.print("Enter rows : ");
        row = s.nextInt();
        //printing row value (upper part )
         for (r=1; r<=row ; r++)
        {
            for (c = 1; c <= r; c++)
                 System.out.print("*");
            for (c= r*2; c <= row*2-1; c++)
                System.out.print(" ");
            for (c = 1; c <= r; c++)
                System.out.print("*");
            System.out.println();
        }
        //printing row value (lower part )
        for (r=1; r<=row -1; r++)
        {
            for (c = row -1; c >= r; c--)
                System.out.print("*");
            for (c= 1; c <= r*2; c++)
                System.out.print(" ");
            for (c = row -1; c >= r; c--)
                System.out.print("*");
            System.out.println();
        }
    }
}
Output:

Enter Rows :5
*            *
**         **
***      ***
****   ****
**********
****   ****
***      ***
**         **
*            *

Method-2 : User Input Character

import java.util.*;
public class Main 
{    
    public static void main(String args[])   
    {   
        // taking variable for loop iteration and row .
        int row,r,c,d;
        char k;
        //creating object 
        Scanner s = new Scanner(System.in);
        // entering the number of row
        System.out.print("Enter rows : ");
        row = s.nextInt();
        // entering any random character
        System.out.print("Enter character : ");
        k = s.next().charAt(0);
        //printing row value (upper part )
         for (r=1; r<=row ; r++)
        {
            for (c = 1; c <= r; c++)
                 System.out.print(k);
            for (c= r*2; c <= row*2-1; c++)
                System.out.print(" ");
            for (c = 1; c <= r; c++)
                System.out.print(k);
            System.out.println();
        }
        //printing row value (lower part )
        for (r=1; r<=row -1; r++)
        {
            for (c = row -1; c >= r; c--)
                System.out.print(k);
            for (c= 1; c <= r*2; c++)
                System.out.print(" ");
            for (c = row -1; c >= r; c--)
                System.out.print(k);
            System.out.println();
        }
    }
}
Output:

Enter Rows :5
Enter character : 0
0                0
00            00
000        000
0000    0000
0000000000
0000    0000
000       000
00           00
0               0

C Code:

#include <stdio.h>
int main() {
   int r, row, c ,d;
   printf("Enter rows: ");
   scanf("%d", &row);
          for (r=1; r<=row ; r++)
        {
            for (c = 1; c <= r; c++)
                 printf("*");
            for (c= r*2; c <= row*2-1; c++)
                 printf(" ");
            for (c = 1; c <= r; c++)
                 printf("*");
            printf("\n");
        }
        //printing row value (lower part )
        for (r=1; r<=row -1; r++)
        {
            for (c = row -1; c >= r; c--)
               printf("*");
            for (c= 1; c <= r*2; c++)
                 printf(" ");
            for (c = row -1; c >= r; c--)
                 printf("*");
             printf("\n");
        }
   return 0;
}
Output:

Enter Rows :5
*            *
**         **
***      ***
****   ****
**********
****   ****
***      ***
**         **
*            *

C++ Code:

#include <iostream>
using namespace std;
int main()
{
   int row, r , c ,d ;
   cout << "Enter  rows: ";
   cin >> row;
    for (r=1; r<=row ; r++)
        {
            for (c = 1; c <= r; c++)
                 cout << "*";
            for (c= r*2; c <= row*2-1; c++)
                 cout <<" ";
            for (c = 1; c <= r; c++)
                cout <<"*";
            cout <<"\n";
        }
        //printing row value (lower part )
        for (r=1; r<=row -1; r++)
        {
            for (c = row -1; c >= r; c--)
             cout <<"*";
            for (c= 1; c <= r*2; c++)
                cout <<" ";
            for (c = row -1; c >= r; c--)
                cout <<"*";
             cout <<"\n";
        }
    return 0;
    
}


Output:

Enter Rows :5
*            *
**         **
***      ***
****   ****
**********
****   ****
***      ***
**         **
*            *

Related Java Star Pattern Programs: