Print Double Headed Arrow Number Pattern
In the previous article, we have discussed Java Program to Print Hollow Diamond Number Pattern
In this article we are going to see how to print hollow diamond number pattern.
Example-1 When rows value = 5 1 2 1 1 2 3 2 1 1 2 3 2 1 1 2 1
Example-2: When rows value=7 1 2 1 1 2 3 2 1 1 2 3 4 3 2 1 1 2 3 4 3 2 1 1 2 3 2 1 1 2 1
Now, let’s see the actual program to print it.
Don’t miss the chance of Java programs examples with output pdf free download as it is very essential for all beginners to experienced programmers for cracking the interviews.
Java Code to Print Double Headed Arrow Number Pattern
import java.util.Scanner; class pattern { public static void main(String[] args) { //Create a new Scanner object Scanner scan = new Scanner(System.in); //Taking total number of rows as input from user System.out.print("Rows : "); int rows= scan.nextInt(); //Row and column are the iterators and counter to print int numberOfRows=1, numberOfColumns; // noValues is the number of values int noValues = 1; // noSpaces is the number of spaces int noSpaces = rows - 1; int noSpaces2 = -1; int val1 = numberOfRows; int val2 = 1; while (numberOfRows <= rows) { // Prints space //space is the count of spaces int space = 1; while (space <= noSpaces) { System.out.print(" "); space = space + 1; } //Prints number // countNum is the count of numbers to be printed int countNum1 = 1; while (countNum1 <= noValues) { System.out.print(val1+" "); val1 = val1 - 1; countNum1 = countNum1 + 1; } // Prints space //space is the count of spaces int space2 = 1; while (space2 <= noSpaces2) { System.out.print(" "); space2 = space2 + 1; } //Prints number // countNum is the count of numbers to be printed if (numberOfRows != 1 && numberOfRows != rows) { countNum1 = 1; while (countNum1 <= noValues) { System.out.print(val2+" "); val2 = val2 + 1; countNum1 = countNum1 + 1; } } System.out.println(); //Goes to the next row if (numberOfRows <= rows / 2) { noValues = noValues + 1; noSpaces = noSpaces - 2; noSpaces2 = noSpaces2 + 2; val1 = numberOfRows + 1; val2 = 1; } else { noValues = noValues - 1; noSpaces = noSpaces + 2; noSpaces2 = noSpaces2 - 2; val1 = rows - numberOfRows; val2 = 1; } numberOfRows = numberOfRows + 1; } } }
Output:: Rows : 7 1 2 1 1 2 3 2 1 1 2 3 4 3 2 1 1 2 3 4 3 2 1 1 2 3 2 1 1 2 1
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 Number Pattern Programs: