Java Program to Print Diamond Number Pattern

Print Diamond Number Pattern

In the previous article, we have discussed Java Program to Print Sand Glass Number Pattern

In this article we are going to see how to print diamond number pattern.

Example-1

When rows value = 5

        1
      123
    12345
   1234567
 123456789
123456789
  1234567
    12345
      123
        1

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 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 loop one to print space and the other one to print the number.
  • After each iteration print a new line.

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

Output:

Rows : 5

       1
     123
   12345
  1234567
123456789
  1234567
   12345
     123
       1

C Code to Print 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 = numberOfRows; numberOfColumns < rows; numberOfColumns++)
      {
         printf(" ");
      }
      //inner loop to print the number
      for (numberOfColumns = 1; numberOfColumns < numberOfRows * 2; numberOfColumns++)
      {
         printf("%d", numberOfColumns);
      }
      //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 = numberOfRows; numberOfColumns < rows; numberOfColumns++)
      {
         printf(" ");
      }
      //inner loop to print the number
      for (numberOfColumns = 1; numberOfColumns < numberOfRows * 2; numberOfColumns++)
      {
         printf("%d", numberOfColumns);
      }
      //Prints a newline
      printf("\n");
   }
   return 0;
}
Output:

Rows : 5

        1
      123
    12345
  1234567
123456789
  1234567
    12345
      123
        1

C++ Code to Print 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 = numberOfRows; numberOfColumns < rows; numberOfColumns++)
        {
            cout << " ";
        }
        //inner loop to print the number
        for (numberOfColumns = 1; numberOfColumns < numberOfRows * 2; numberOfColumns++)
        {
            cout << numberOfColumns;
        }
        //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 = numberOfRows; numberOfColumns < rows; numberOfColumns++)
        {
            cout << " ";
        }
        //inner loop to print the number
        for (numberOfColumns = 1; numberOfColumns < numberOfRows * 2; numberOfColumns++)
        {
            cout << numberOfColumns;
        }
        //Prints a newline
        cout << endl;
    }
    return 0;
}

Output:

Rows : 5

       1
     123
    12345
  1234567
123456789
  1234567
    12345
      123
        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: