Java Program to Print Less Than Symbol Character Pattern

Printing Less Than Symbol Character Pattern

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

In this article we are going to see how to print the less than symbol character pattern.

Example-1

When size value=7

         D
      C
   B
A
    B
       C
          D
Example-2

When size value=5

      C
   B
A
   B
      C

Now, let’s see the actual program to print it.

Don’t stop learning now. Get hold of all the important Java fundamentals with the Simple java program example guide and practice well.

Approach:

  • Enter size of the pattern and store it in an integer variable size.
  • Take one outer for loop to iterate the rows.
  • Take one inner for loops, to iterate the columns.
  • After each iteration print a new line.

Java Code to Print Less Than Symbol Character Pattern

import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
    // variable 'size' refers to number of throws 
    // variable 'r' and 'c' are used to iterate the for loop for rows and columns
    int size, r, c;
    // Scanner class object created
    Scanner scan = new Scanner(System.in);
    //Taking size as input from user
    System.out.print("Size : ");
    size = scan.nextInt();
    //Taking middle of the pattern
    int mid = size / 2 + 1;
    // ASCII value taken 64 
    int asciiAlpha = 64;
    
    //Outer Loop 
    //This for loop will iterate all the rows
    for (r = 1; r <= size; r++)
    {
        //Inner loop 
        //This for loop is used to iterate all the columns 
        //During iteration it will print the column values for each row
        for (c = 1; c <= size; c++)
        {   
            // if c value matches with mid value then it prints the character
            if (c == mid)
                // Printing character
                System.out.print((char)(c + asciiAlpha));
            else
                // Printing space
                System.out.print("  ");
        }
        //Prints a new line
        System.out.println();
        //Adjusting the mid value
        if (r <= size / 2)
            mid--;
        else
            mid++;
    }
  }
}
Output:

Size : 7
 
        D
     C
   B
A
   B
     C
        D

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: