Java Program to Print Hollow Lower Triangular Star Pattern

Program to Print Hollow Lower Triangular Star Pattern

In this article we are going to see how to print Hollow lower triangular star program.

Example-1

When row value=10

 * * * * * * * * * *
 * * * * * * * * * *
 *   * * * * * * * *
 *      * * * * * * *
 *        * * * * * *
 *           * * * * *
 *             * * * *
 *                * * *
 *                  * *
 * * * * * * * * * *

Example-2:

When row value=15

 * * * * * * * * * * * * * * *
 * * * * * * * * * * * * * * *
 *   * * * * * * * * * * * * *
 *      * * * * * * * * * * * *
 *        * * * * * * * * * * *
 *           * * * * * * * * * *
 *             * * * * * * * * *
 *                * * * * * * * *
 *                  * * * * * * *
 *                     * * * * * *
 *                       * * * * *
 *                          * * * *
 *                            * * *
 *                               * *
 * * * * * * * * * * * * * * *

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

Guys who are serious about learning the concepts of the java programming language should practice this list of programs in java and get a good grip on it for better results in exams or interviews.

Approach:

  • Enter total row and store it in an integer variable row.
  • Take first outer for loop to keep track of number of rows.
  • Take first inner for loop to print space by wave height.
  • Take second inner for loop for keep track of number of column  and will print the stars according to the condition if (r == row || c == 1) else will print spaces .
  • 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=1;
    //creating object 
    Scanner s = new Scanner(System.in);
    // entering the number of row
    System.out.print("Enter rows : ");
    row = s.nextInt();
    // loop to keep track of number of rows
    for (r = 1; r <= row; r++)
    {
        // loop track number of columns
        for ( c = 1; c <=row; c++) 
            {
               if (r == row || c == 1)
                    System.out.print(" *");
                else if (c < d)
                    System.out.print("  ");
 
                //  printing  star in remaining portion
                else
                    System.out.print(" *");
            }
            System.out.println();
            d++;
     }
  }
}
Output :

Enter row :  10

 * * * * * * * * * *
 * * * * * * * * * *
 *   * * * * * * * *
 *      * * * * * * *
 *        * * * * * *
 *           * * * * *
 *              * * * *
 *                * * *
 *                   * *
 * * * * * * * * * *

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=1;
    //creating object 
    Scanner s = new Scanner(System.in);
    // entering the number of row
    System.out.print("Enter rows : ");
    row = s.nextInt();
     // entering any character
    System.out.print("Enter character : ");
    char lower = s.next().charAt(0);
    // loop to keep track of number of rows
    for (r = 1; r <= row; r++)
    {
        // loop track number of columns
        for ( c = 1; c <=row; c++) 
            {
               if (r == row || c == 1)
                    System.out.print(" "+lower);
                else if (c < d)
                    System.out.print("  ");
 
                //  printing  star in remaining portion
                else
                    System.out.print(" "+lower);
            }
            System.out.println();
            d++;
     }
  }
}
Output :

Enter row :  10
Enter character : *

 * * * * * * * * * *
 * * * * * * * * * *
 *    * * * * * * * *
 *      * * * * * * *
 *         * * * * * *
 *           * * * * *
 *              * * * *
 *                * * *
 *                   * *
 * * * * * * * * * *

C Code:

#include <stdio.h>
int main() 
{
    int row,r,c,d=1;
    printf("Enter rows: ");
    scanf("%d", &row);
     for (r = 1; r <= row; r++)
    {
        for ( c = 1; c <=row; c++) 
            {
               if (r == row || c == 1)
                     printf(" *");
                else if (c < d)
                     printf("  ");
                else
                     printf(" *");
            }
             printf("\n");
            d++;
     }
   return 0;
}

Output :

Enter row :  10

 * * * * * * * * * *
 * * * * * * * * * *
 *   * * * * * * * *
 *      * * * * * * *
 *        * * * * * *
 *           * * * * *
 *             * * * *
 *               * * *
 *                  * *
 * * * * * * * * * *

C++ Code:

#include <iostream>
using namespace std;
int main()
{
    int row,r,c,d=1;
    cout <<"Enter rows: ";
    cin >> row ;
    for (r = 1; r <= row; r++)
    {
        for ( c = 1; c <=row; c++) 
            {
               if (r == row || c == 1)
                    cout << " *";
                else if (c < d)
                     cout <<"  ";
                else
                     cout <<" *";
            }
             cout << "\n";
            d++;
     }
   return 0;
}
Output :

Enter row :  10

 * * * * * * * * * *
 * * * * * * * * * *
 *   * * * * * * * *
 *      * * * * * * *
 *        * * * * * *
 *           * * * * *
 *             * * * *
 *                * * *
 *                  * *
 * * * * * * * * * *

Related Java Star Pattern Programs: