Java Program to Print Downward Arrow Mark Symbol Number Pattern

Print Downward Arrow Mark Symbol Number Pattern

In this article we are going to see how to print the downward arrow mark symbol number pattern.

Exampe-1

When size value= 5

   3  
   3  
1 3 5
 234 
   3
Example-2

When size value= 9

     5    
     5    
     5    
     5    
1   5   9
 2  5  8 
  3 5 7  
   456   
     5

Now, let’s see the actual program to print 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 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:

import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
    int size, r, c;
    //prefer odd number
    //Taking size as input from user
    System.out.print("Size : ");
    Scanner scan = new Scanner(System.in);
    size = scan.nextInt();
    //Taking middle of the pattern in negative
    int mid = -size / 2 + 1;
    //Outer Loop
    for (r = 1; r <= size; r++)
    {
        //Inner loop
        for (c = 1; c <= size; c++)
        {
            if (c == size / 2 + 1 || c == mid || c == size - mid + 1)
                System.out.print(c);
            else
                System.out.print(" ");
        }
        //Prints a newline
        System.out.println();
        //Incrementing the mid value
        mid++;
    }
  }
}
Output:

Size :  9
  
     5    
     5    
     5    
     5    
1   5   9
 2  5  8 
  3 5 7  
   456   
     5

C CODE:

#include <stdio.h>
int main()
{
    int size, r, c;
    //Taking size as input from user
    printf("Size : ");
    scanf("%d", &size);
    //Taking middle of the pattern in negative
    int mid = -size / 2 + 1;
    //Outer Loop
    for (r = 1; r <= size; r++)
    {
        //Inner loop
        for (c = 1; c <= size; c++)
        {
            if (c == size / 2 + 1 || c == mid || c == size - mid + 1)
                printf("%d",c);
            else
                printf(" ");
        }
        //Prints a newline
        printf("\n");
        //incrementing the mid value
        mid++;
    }
    return 0;
}
Output:

Size :  9

     5    
     5    
     5    
     5    
1   5   9
 2  5  8 
  3 5 7  
   456   
     5

C++ CODE:

#include <iostream>
using namespace std;
int main(int argc, char const *argv[])
{
    int size, r, c;
    //Taking size as input from user
    cout << "Size : ";
    cin >> size;
    //Taking middle of the pattern in negative
    int mid = -size / 2 + 1;
    //Outer Loop
    for (r = 1; r <= size; r++)
    {
        //Inner loop
        for (c = 1; c <= size; c++)
        {
            if (c == size / 2 + 1 || c == mid || c == size - mid + 1)
                cout << c;
            else
                cout << " ";
        }
        //Prints a newline
        cout << endl;
        //Incrementing the mid value
        mid++;
    }
    return 0;
}
Output:

Size :  9
  
     5    
     5    
     5    
     5    
1   5   9
 2  5  8 
  3 5 7  
   456   
     5