Java Program to Print Inverted Pant Style Star Pattern

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

Example-1

When rows value = 5
*                    *
* *               * *
* * *          * * *
* * * *     * * * *
* * * * * * * * * *
Example-2

When rows value = 4

*                    *
* *               * *
* * *          * * *
* * * *     * * * *

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

Are you a job seeker and trying to find simple java programs for Interview? This would be the right choice for you, just tap on the link and start preparing the java programs covered to crack the interview.

Approach:

  • Enter the number of rows to print and store it in an integer variable rows.
  • Take first for loop to print all rows.
  • Take inner for loop to print column values and one to print empty spaces.
  • Then go on printing the stars according to the iteration.

JAVA Code:

Method-1 : Static Star Character

import java.util.Scanner;
class Main
{
    public static void main(String[] args)  
    {  
        Scanner scan = new Scanner(System.in);
        System.out.print("Rows : ");
        //Taking total rows as input from user
        int r,s, c, rows = scan.nextInt();

        for(r = rows; r>=0; r--)
        {
            for(c = rows ; c>r; c--)
            //Inner loop that prints first half stars
                System.out.print("* ");
            for(s = 1; s<=4*r;s++)
            //Inner loop that prints space in between
                System.out.print(" ");
            for(c = r+1 ; c<=rows; c++)
            //Inner loop that prints second half stars
                System.out.print("* ");
    //Prints a new line
            System.out.println();
        }
    }
}

Output:

Rows : 5
*                     *
* *                * *
* * *           * * *
* * * *      * * * *
* * * * * * * * * *

Method-2 : User Input Character

import java.util.Scanner;
class Main
{
    public static void main(String[] args)  
    {  
        Scanner scan = new Scanner(System.in);
        System.out.print("Rows : ");
        //Taking total rows as input from user
        int r,s, c, rows = scan.nextInt();
        System.out.print("Character : ");
        // entering any random character
        char q=scan.next().charAt(0);

        for(r = rows; r>=0; r--)
        {
            for(c = rows ; c>r; c--)
            //Inner loop that prints first half stars
                System.out.print(q+" ");
            for(s = 1; s<=4*r;s++)
            //Inner loop that prints space in between
                System.out.print(" ");
            for(c = r+1 ; c<=rows; c++)
            //Inner loop that prints second half stars
                System.out.print(q+" ");
    //Prints a new line
            System.out.println();
        }
    }
}

Output:

Rows : %
Character : %
%                               %
% %                        % %
% % %                % % %
% % % %         % % % %
% % % % % % % % % %

C Code:

#include <stdio.h>
int main(int argc, char const *argv[])
{
    printf("Rows : ");
    //Taking rows as input from user
    int rows, s, r, c;
    scanf("%d", &rows);

    for (r = rows - 1; r >= 0; r--)
    {
        for (c = rows; c > r; c--)
            //Inner loop that prints first half stars
            printf("* ");
        for (s = 1; s <= 4 * r; s++)
            //Inner loop that prints space in between
            printf(" ");
        for (c = r + 1; c <= rows; c++)
            //Inner loop that prints second half stars
            printf("* ");
        //Prints a new line
        printf("\n");
    }
    return 0;
}
Output:

Rows : 5
*                     *
* *                * *
* * *           * * *
* * * *      * * * *
* * * * * * * * * *

C++ Code:

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

    for (r = rows - 1; r >= 0; r--)
    {
        for (c = rows; c > r; c--)
            //Inner loop that prints first half stars
            cout << "*" << " ";
        for (s = 1; s <= 4 * r; s++)
            //Inner loop that prints space in between
            cout << " ";
        for (c = r + 1; c <= rows; c++)
            //Inner loop that prints second half stars
            cout << "*" << " ";
        //Prints a new line
        cout << endl;
    }
    return 0;
}
Output:

Rows : 5
*                     *
* *                * *
* * *           * * *
* * * *      * * * *
* * * * * * * * * *

Related Java Star Pattern Programs: