Java Program to Print Trapezium Number Pattern

Print Trapezium Number Pattern

In the previous article, we have discussed Java Program to Print Alphabet N Number Pattern

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

Example-1

Rows : 3

1 2 3 4 10 11 12 13
 5 6 7 9 10 11
   8 9 9 10

Access the Simple Java program for Interview examples with output from our page and impress your interviewer panel with your coding skills.

Approach:

  • Enter total number of rows and store it in an integer variable rows
  • Take tone outer for loop to iterate the rows.
  • Take three inner for loops, one to print space and other to print left and right sides.
  • After each iteration print a new line.

Java Code to Print Trapezium 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;

    //Variables to store print numbers
   //Left is the left side and right is the right side of the pattern
   int left = 1, right = rows * rows + 1;

   for (numberOfRows = rows; numberOfRows > 0; numberOfRows--)
   {
      //Loop to print space
      for (numberOfColumns = rows; numberOfColumns > numberOfRows; numberOfColumns--)
      {
         System.out.print(" ");
      }
      //Loop to print left side
      for (numberOfColumns = 0; numberOfColumns <= numberOfRows; numberOfColumns++)
      {
         System.out.print(left++ + " ");
      }
      //Loop that prints rightside
      for (numberOfColumns = 0; numberOfColumns <= numberOfRows; numberOfColumns++)
      {
         System.out.print(right++ + " ");
      }
      //Setting the right variable after each iteration
      right = right - (numberOfRows - 1) * 2 - 1;
        // Prints a new line
        System.out.println();
    }
}
}

Output:

Rows : 3

1 2 3 4 10 11 12 13
 5 6 7 9 10 11
  8 9 9 10

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:

Leave a Comment