Java Program to Print Pant Number Pattern (First Approach)

Print Pant Number Pattern(First Approach)

In the previous article, we have discussed Java Program to Print Hollow Square with Increasing Number Pattern

In this article we are going to see how to print the invert pant number pattern.

Example-1

When row value=4

4321234
432  234
43      34
4          4
Example-2:

When row value=5

543212345
5432  2345
543      345
54          45
5              5

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

Don’t stop learning now. Get hold of all the important Java fundamentals with the Simple java program example guide and practice well.

Approach:

  • Enter total row and store it in an integer variable rows.
  • Take one outer for loop to iterate the rows.
  • Take two inner for loops, one to print the space and the other one to print the character.
  • After each iteration print a new line.

Java Code to Print Pant 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 number of rows as input from user
    System.out.print("Rows : ");
    int rows = scan.nextInt();

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

    //Outer loop to iterate the rows
    //Iterates from rows entered by the user to number 1
    for (numberOfRows = rows; numberOfRows >= 1; numberOfRows--)
    {
        //Inner loops to iterate the columns
        for (numberOfColumns = rows; numberOfColumns >= 1 + rows - numberOfRows; numberOfColumns--)
        {
            //Prints number
            System.out.print(numberOfColumns);
        }

        for (numberOfColumns = numberOfRows * 2; numberOfColumns < rows * 2 - 1; numberOfColumns++)
        {
            //Prints space
            System.out.print(" ");
        }

        for (numberOfColumns = rows + 1 - numberOfRows; numberOfColumns <= rows; numberOfColumns++)
        {
            //Prints space
            if (numberOfColumns != 1)
                System.out.print(numberOfColumns);
        }
        //Prints a newline
        System.out.println();
    }
}
}


Output:

Rows : 5

543212345
5432  2345
543      345
54          45
5              5

C Code to Print Pant 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
    int numberOfRows, numberOfColumns;

    //Outer loop to iterate the rows
    //Iterates from rows entered by the user to number 1
    for (numberOfRows = rows; numberOfRows >= 1; numberOfRows--)
    {
        //Inner loops to iterate the columns
        for (numberOfColumns = rows; numberOfColumns >= 1 + rows - numberOfRows; numberOfColumns--)
        {
            //Prints number
            printf("%d", numberOfColumns);
        }

        for (numberOfColumns = numberOfRows * 2; numberOfColumns < rows * 2 - 1; numberOfColumns++)
        {
            //Prints space
            printf(" ");
        }

        for (numberOfColumns = rows + 1 - numberOfRows; numberOfColumns <= rows; numberOfColumns++)
        {
            //Prints space
            if (numberOfColumns != 1)
                printf("%d", numberOfColumns);
        }
        //Prints a newline
        printf("\n");
    }
    return 0;
}

Output:

Rows : 5

543212345
5432  2345
543      345
54          45
5              5

C++ Code to Print Pant Number Pattern

#include <iostream>
using namespace std;
int main(int argc, char const *argv[])
{
    //Taking total number of rows as input from user
    printf("Rows : ");
    int rows;
    cin >> rows;

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

    //Outer loop to iterate the rows
    //Iterates from rows entered by the user to number 1
    for (numberOfRows = rows; numberOfRows >= 1; numberOfRows--)
    {
        //Inner loops to iterate the columns
        for (numberOfColumns = rows; numberOfColumns >= 1 + rows - numberOfRows; numberOfColumns--)
        {
            //Prints number
            Cout<<numberOfColumns;
        }

        for (numberOfColumns = numberOfRows * 2; numberOfColumns < rows * 2 - 1; numberOfColumns++)
        {
            //Prints space
            cout <<" ";
        }

        for (numberOfColumns = rows + 1 - numberOfRows; numberOfColumns <= rows; numberOfColumns++)
        {
            //Prints space
            if (numberOfColumns != 1)
                cout<< numberOfColumns;
        }
        //Prints a newline
        cout << endl;
    }
    return 0;
}
Output:

Rows : 5

543212345
5432  2345
543      345
54          45
5              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:

Leave a Comment