Java Program to Print Pant Number Pattern (Second Approach)

Print Pant Number Pattern(Second Approach)

In the previous article, we have discussed Java Program to Print Pant Number Pattern(First Approach)

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

Example-1

When row value=4

1234321
123  321
12      21
1          1
Example-2:

When row value=5

123454321
1234  4321
123      321
12          21
1             1

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

Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners tutorial.

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

Java Program to Print Pant Number Pattern Second Approach

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 = 1; numberOfColumns <= numberOfRows; numberOfColumns++)
        {
            //Prints number
            System.out.print(numberOfColumns);
        }

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

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

Rows : 4

1234321
123  321
12      21
1          1

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 = 1; numberOfColumns <= numberOfRows; numberOfColumns++)
        {
            //Prints number
            printf("%d", numberOfColumns);
        }

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

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

Output:

Rows : 4

1234321
123  321
12      21
1          1

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 = 1; numberOfColumns <= numberOfRows; numberOfColumns++)
        {
            //Prints number
            cout<< numberOfColumns;
        }

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

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

Output:

Rows : 4

1234321
123  321
12      21
1          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: