Java Program to Print Downward Arrow Mark Character Pattern

Printing Downward Arrow Mark Character Pattern

In the previous article, we have discussed Java Program to Print Upward Arrow Mark Symbol Character Pattern

In this article we will see how to print Print Downward Arrow Mark Character Pattern.

Example-1 

When arrow height is 5

    C
    C
A  C  E
  BCD
    C
Example-2

When arrow height is 7

      D
      D
      D
A    D   G
  B  D  F
    CDE
      D

Now, let’s see the program to print.

Approach:

  1. Take an integer  variable size to store the height of arrow.
  2. Then take a for loop to iterate all the rows where height represents the number of rows.
  3. Take an inner for loop to print the character and space based on the if condition inside inner for loop.
  4. After printing values for one row move to next line.

Java Code to Print Downward Arrow Mark Character Pattern

import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
    // 'size' variable refers the hight or number of rows in arrow
    // 'r' variable is taken to iterate the first for loop 
    // which is reponsible to iterate the rows
    // 'c' variable taken to iterate the inner for loop 
    // which is responsible to print the column values
    int size, r, c;
    //Object of Scanner class created
    Scanner scan = new Scanner(System.in);
    
    // Taking size as input from user
    System.out.print("Size(Prefer odd number) : ");
    size = scan.nextInt();
    
    // Taking middle row of the pattern
    int mid = -size / 2 + 1;
    
    // ASCII value taken 64
    int asciiAlpha = 64;
    
    // Outer Loop
    // This loop is responsible to iterate all rows
    // When printing of a single row will be completed then
    // it will go to the next row
    // This loop will iterate from 'r' value 1 to r <= size
    for (r = 1; r <= size; r++)
    {
        // Inner loop
        // This loop is responsible to print column values in a row
        // This loop will iterate from 'c' value 1 to c <= size
        for (c = 1; c <= size; c++)
        {
            if (c == size / 2 + 1 || c == mid || c == size - mid + 1)
                // Based on the above if condition, 
                // if any condition matches then character will be printed
                // As column value i.e 'c' value starts from 1 
                // (c value 1 + asciiAlpha value 64 so total 65 which represents Character 'A')
                // So for each row first character value is 'A'
                // And it will go on printing the corresponding character based on condition.
                System.out.print((char)(c + asciiAlpha));
            else
                // If the 'if condition' fails 
                // then 'if block' will not be executed
                // And else block will be executed
                // else block will print the space
                System.out.print(" ");
        }
        //Prints a newline
        System.out.println();
        //Incrementing the mid value
        mid++;
    }
  }
}
Output:

Size(Prefer odd number) : 7

      D
      D
      D
A    D   G
  B  D  F
    CDE
      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: