Java Program to Print Upward Arrow Mark Character Pattern

Printing Upward Arrow Mark Character Pattern

In the previous article, we have discussed Java Program to Print Square with Row wise Repeated with Decreasing Order Character Pattern

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

Example-1 

When arrow height is 5

    C
  BCD
A  C  E
    C
    C
Example-2

When arrow height is 7

      D
    CDE
  B  D  F
A    D   G
      D
      D
      D

Now, let’s see the program to print.

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:

  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 Upward 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 new line
        System.out.println();
        //Incrementing the mid value
        mid++;
    }
 }
}
Output:

Size(Prefer odd number) : 7

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