Java Program to Print Sand Glass Number Pattern

Print Sand Glass Number Pattern

In the previous article, we have discussed Java Program to Print Reverse Floyd’s Triangle Number Pattern

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

Example-1

When rows value = 5

1 2 3 4 5
 2 3 4 5
  3 4 5
   4 5
    5
    5
   4 5
  3 4 5
 2 3 4 5
1 2 3 4 5


Example-2:

When rows value=7

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

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

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 Sand Glass Number Pattern

import java.util.Scanner;
class Main
{

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 = 1; numberOfColumns < numberOfRows; numberOfColumns++)
        {
            System.out.print(" ");
        }
        //inner loop to print the number
        for (numberOfColumns = numberOfRows; numberOfColumns <= rows; 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 = 1; numberOfColumns < numberOfRows; numberOfColumns++)
        {
            System.out.print(" ");
        }
        //inner loop to print the number
        for (numberOfColumns = numberOfRows; numberOfColumns <= rows; numberOfColumns++)
        {
           System.out.print(numberOfColumns+" ");
        }
        //Prints a newline
        System.out.println();
    }
}
}
Output:

Rows : 5

1 2 3 4 5
 2 3 4 5
  3 4 5
   4 5
    5
    5
   4 5
  3 4 5
 2 3 4 5
1 2 3 4 5

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

Rows : 5

1 2 3 4 5
 2 3 4 5
  3 4 5
   4 5
    5
    5
   4 5
  3 4 5
 2 3 4 5
1 2 3 4 5

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

Output:

Rows : 5

1 2 3 4 5
 2 3 4 5
  3 4 5
   4 5
    5
    5
   4 5
  3 4 5
 2 3 4 5
1 2 3 4 5

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: