Java Program to Print Pant Style Star Pattern

Program to Print Pant Style Star Pattern

In this article we are going to see how to print the pant style star 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 new to the java programming language? We recommend you to ace up your practice session with these Basic Java Programs Examples

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 = 0; r<rows; 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)  
    {  
        int r,s, c, rows;
        char p;
        Scanner scan = new Scanner(System.in);
        System.out.print("Rows : ");
        //Taking total rows as input from user
        rows = scan.nextInt();
        System.out.print("Character : ");
        //Taking total rows as input from user
        p = scan.next().charAt(0);

        for(r = 0; r<rows; r++)
        {
            for(c = rows ; c>r; c--)
            //Inner loop that prints first half stars
                System.out.print(p+" ");
            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(p+" ");
        	//Prints a new line
            System.out.println();
        }
    }
}


Output:

Rows : 5
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 = 0; r < rows; 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 = 0; r < rows; 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: