Java Program to Print Hollow Diamond Number Pattern

Print Hollow Diamond Number Pattern

In the previous article, we have discussed Java Program to Print 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   2
   3       3
 4           4
5               5
 4           4
   3       3
     2   2
       1

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

If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list available.

Approach:

  • Enter total number of rows and store it in an integer variable rows
  • Take two outer for loops(for both halves) to iterate the rows.
  • Take two inner for loops to print both space and number.
  • After each iteration print a new line.

Java Code to Print Hollow Diamond 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, numberOfColumns;

    //Outer loop to print the lower half
   //Iterates from 1 to the number of rows entered by the user
   for (numberOfRows = 1; numberOfRows <= rows; numberOfRows++)
   {
      //Inner loop to print the space
      for (numberOfColumns = rows; numberOfColumns >= (numberOfRows - 1) * 2 - 1; numberOfColumns--)
      {
        System.out.print(" ");
      }
      System.out.print(numberOfRows);
      //inner loop to print the number
      for (numberOfColumns = 2; numberOfColumns <= (numberOfRows - 1) * 4; numberOfColumns++)
      {
        System.out.print(" ");
      }
      if (numberOfRows > 1)
         System.out.print(numberOfRows);
      //Prints a newline
      System.out.println();
   }
   //Outer loop to print the lower half
   //Iterates from number of rows-1 entered by user to 1
   for (numberOfRows = rows - 1; numberOfRows >= 1; numberOfRows--)
   {
      //Inner loop to print the space
      for (numberOfColumns = rows; numberOfColumns >= (numberOfRows - 1) * 2 - 1; numberOfColumns--)
      {
        System.out.print(" ");
      }
      System.out.print(numberOfRows);
      //inner loop to print the number
      for (numberOfColumns = 2; numberOfColumns <= (numberOfRows - 1) * 4; numberOfColumns++)
      {
        System.out.print(" ");
      }
      if (numberOfRows > 1)
         System.out.print(numberOfRows);
      //Prints a newline
      System.out.println();
   }
}
}

Output

Rows : 7

           1
         2   2
       3       3
     4           4
   5               5
  6                   6
7                       7
  6                   6
   5               5
     4           4
       3       3
         2   2
           1

C Code to Print Hollow Diamond Number Pattern

#include <stdio.h>

int main()
{
   //Taking total number of rows as input from user
   printf("Rows : ");
   int rows;
   scanf("%d", &rows);

   //Row and column are the iterators and counter to print
   int numberOfRows, numberOfColumns;

   //Outer loop to print the lower half
   //Iterates from 1 to the number of rows entered by the user
   for (numberOfRows = 1; numberOfRows <= rows; numberOfRows++)
   {
      //Inner loop to print the space
      for (numberOfColumns = rows; numberOfColumns >= (numberOfRows - 1) * 2 - 1; numberOfColumns--)
      {
         printf(" ");
      }
      printf("%d", numberOfRows);
      //inner loop to print the number
      for (numberOfColumns = 2; numberOfColumns <= (numberOfRows - 1) * 4; numberOfColumns++)
      {
         printf(" ");
      }
      if (numberOfRows > 1)
         printf("%d", numberOfRows);
      //Prints a newline
      printf("\n");
   }
   //Outer loop to print the lower half
   //Iterates from number of rows-1 entered by user to 1
   for (numberOfRows = rows - 1; numberOfRows >= 1; numberOfRows--)
   {
      //Inner loop to print the space
      for (numberOfColumns = rows; numberOfColumns >= (numberOfRows - 1) * 2 - 1; numberOfColumns--)
      {
         printf(" ");
      }
      printf("%d", numberOfRows);
      //inner loop to print the number
      for (numberOfColumns = 2; numberOfColumns <= (numberOfRows - 1) * 4; numberOfColumns++)
      {
         printf(" ");
      }
      if (numberOfRows > 1)
         printf("%d", numberOfRows);
      //Prints a newline
      printf("\n");
   }
   return 0;
}
Output:

Rows : 7

            1
          2   2
        3       3
      4           4
    5               5
  6                   6
7                       7
  6                   6
    5               5
     4           4
       3       3
         2   2
           1

C++ Code to Print Hollow Diamond Number Pattern

#include <iostream>
using namespace std;

int main(int argc, char const *argv[])
{
    //Taking total number of rows as input from user
    cout << "Rows : ";
    int rows;
    cin >> rows;

    //Row and column are the iterators and counter to print
    int numberOfRows, numberOfColumns;

    //Outer loop to print the lower half
    //Iterates from 1 to the number of rows entered by the user
    for (numberOfRows = 1; numberOfRows <= rows; numberOfRows++)
    {
        //Inner loop to print the space
        for (numberOfColumns = rows; numberOfColumns >= (numberOfRows - 1) * 2 - 1; numberOfColumns--)
        {
            cout << " ";
        }
        cout << numberOfRows;
        //inner loop to print the number
        for (numberOfColumns = 2; numberOfColumns <= (numberOfRows - 1) * 4; numberOfColumns++)
        {
            cout << " ";
        }
        if (numberOfRows > 1)
            cout << numberOfRows;
        //Prints a newline
        cout << endl;
    }
    //Outer loop to print the lower half
    //Iterates from number of rows-1 entered by user to 1
    for (numberOfRows = rows - 1; numberOfRows >= 1; numberOfRows--)
    {
        //Inner loop to print the space
        for (numberOfColumns = rows; numberOfColumns >= (numberOfRows - 1) * 2 - 1; numberOfColumns--)
        {
            cout << " ";
        }
        cout << numberOfRows;
        //inner loop to print the number
        for (numberOfColumns = 2; numberOfColumns <= (numberOfRows - 1) * 4; numberOfColumns++)
        {
            cout << " ";
        }
        if (numberOfRows > 1)
            cout << numberOfRows;
        //Prints a newline
        cout << endl;
    }
    return 0;
}
Output:

Rows : 7

           1
         2   2
       3       3
     4           4
   5               5
  6                   6
7                       7
  6                   6
   5               5
     4           4
       3       3
         2   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: