Java Program to Print Hollow Lower Triangular Character Pattern

Print Hollow Lower Triangular Character Pattern

In the previous article, we have discussed Java Program to Print Hexagonal Character Pattern

In this article we are going to see how to print Hollow lower triangular character pattern.

Example-1

When number of rows = 10

 A B C D E F G H I J
 A B C D E F G H I J
 A    C D E F G H I J
 A       D E F G H I J
 A           E F G H I J
 A              F G H I J
 A                 G H I J
 A                    H I J
 A                        I J
 A B C D E F G H I J

 Example-2

When number of rows = 5

 A B C D E
 A B C D E
 A    C D E
 A       D E
 A B C D E

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

Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.

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 characters according to the condition if (r == row || c == 1) else will print spaces .
  • Then go on printing the character according to loop.

Java Code to Print Hollow Lower Triangular Character Pattern

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;
    // Starting ASCII value taken 64 
    int asciiAlpha = 64;
    //creating object  of Scanner class
    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(" "+(char)(c + asciiAlpha));
                else if (c < d)
                    System.out.print("  ");
 
                //  printing  star in remaining portion
                else
                    System.out.print(" "+(char)(c + asciiAlpha));
            }
            System.out.println();
            d++;
     }
  }
}
Output:

Enter rows :  10

 A B C D E F G H I J
 A B C D E F G H I J
 A    C D E F G H I J
 A       D E F G H I J
 A          E F G H I J
 A             F G H I J
 A                G H I J
 A                   H I J
 A                       I J
 A B C D E F G H I J

C Code to Print Hollow Lower Triangular Character Pattern

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

Enter rows :  10

 A B C D E F G H I J
 A B C D E F G H I J
 A    C D E F G H I J
 A       D E F G H I J
 A          E F G H I J
 A             F G H I J
 A                G H I J
 A                   H I J
 A                       I J
 A B C D E F G H I J

Are you seeking professional help for coding in the Java programming language? The tutorial of Java Programming Examples for beginners and experts will strongly improve your coding skills then you can program for any logic in Java.

Related Java Character Pattern Programs:

Leave a Comment