Java Program to Print Hollow Left Pascal’s Triangle Star Pattern

Program to Print Hollow Left Pascal’s Triangle Star Pattern

In this article we are going to see how to print hollow left pascal’s Tringle  star program.

Example-1

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

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

Now, let’s see the actual program printing  it.

Want to excel in java coding? Practice with these Java Programs examples with output and write any kind of easy or difficult programs in the java language

Approach:

  • Enter total row and store it in an integer variable row.
  • Take first outer for loop to print following.
    • Take  first inner for loop to print space in decreasing order .
    • Take second inner for loop to print stars in increasing order with the condition
      if( c == 1 || c == r )
  • Take second outer for loop for printing following.
    • Take  first inner for loop to print space in increasing order .
    • Take second inner for loop to print stars in decreasing order with the condition
      if( c == row-1 || c == r )
  • Then go on printing the star symbol according to loop.

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();
    //outer for loop 
    for (r = 1; r <= row; r++)
        {
            // Print space in decreasing order
            for ( c = row; c > r; c--)
            {
                System.out.print(" ");
            }
            // Print star in increasing order
            for ( c = 1; c <= r; c++)
            {
                if( c == 1 || c == r )
                    System.out.print("*");
                else
                    System.out.print(" ");
            }
            System.out.println();
        }
        for ( r = 1; r <= row-1; r++)
        {
            // Print space in increasing order
            for ( c = 1; c <= r; c++)
            {
                System.out.print(" ");
            }
            // Print star in decreasing order
            for ( c = row-1; c >= r; c--)
            {
                if( c == row-1 || c == r )
                    System.out.print("*");
                else
                    System.out.print(" ");
            }
            System.out.println();
        } 
    }
}


Output :

Enter Row : 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 random;
    //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 any character : ");
    random = s.next().charAt(0);
    //outer for loop 
    for (r = 1; r <= row; r++)
        {
            // Print space in decreasing order
            for ( c = row; c > r; c--)
            {
                System.out.print(" ");
            }
            // Print star in increasing order
            for ( c = 1; c <= r; c++)
            {
                if( c == 1 || c == r )
                    System.out.print(random);
                else
                    System.out.print(" ");
            }
            System.out.println();
        }
        for ( r = 1; r <= row-1; r++)
        {
            // Print space in increasing order
            for ( c = 1; c <= r; c++)
            {
                System.out.print(" ");
            }
            // Print star in decreasing order
            for ( c = row-1; c >= r; c--)
            {
                if( c == row-1 || c == r )
                    System.out.print(random);
                else
                    System.out.print(" ");
            }
            System.out.println();
        } 
    }
}


Output :

Enter Row : 5

Enter any character : %
      %
   %%
  % %
 %  %
%   %
 %  %
  % %
   %%
      %

C Code:

#include <stdio.h>
int main() 
{
   int r, row, c ,d;
   printf("Enter rows: ");
   scanf("%d", &row);
   for (r = 1; r <= row; r++)
        {
            // Print space in decreasing order
            for ( c = row; c > r; c--)
            {
                printf(" ");
            }
            // Print star in increasing order
            for ( c = 1; c <= r; c++)
            {
                if( c == 1 || c == r )
                   printf("*");
                else
                    printf(" ");
            }
            printf("\n");
        }
        for ( r = 1; r <= row-1; r++)
        {
            // Print space in increasing order
            for ( c = 1; c <= r; c++)
            {
               printf(" ");
            }
            // Print star in decreasing order
            for ( c = row-1; c >= r; c--)
            {
                if( c == row-1 || c == r )
                    printf("*");
                else
                   printf(" ");
            }
            printf("\n");
        } 
   return 0;
}
Output :

Enter Row : 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++)
        {
            // Print space in decreasing order
            for ( c = row; c > r; c--)
            {
                cout <<" ";
            }
            // Print star in increasing order
            for ( c = 1; c <= r; c++)
            {
                if( c == 1 || c == r )
                    cout <<"*";
                else
                    cout <<" ";
            }
            cout <<"\n";
        }
        for ( r = 1; r <= row-1; r++)
        {
            // Print space in increasing order
            for ( c = 1; c <= r; c++)
            {
                cout <<" ";
            }
            // Print star in decreasing order
            for ( c = row-1; c >= r; c--)
            {
                if( c == row-1 || c == r )
                    cout <<"*";
                else
                    cout <<" ";
            }
            cout <<"\n";
        }      
   return 0;
}

Output :

Enter Row : 5
    *
   **
  * *
 *  *
*   *
 *  *
  * *
   **
    *

Related Java Star Pattern Programs: