Java Program to Print Hollow Mirrored Right Triangle Star Pattern

Program to Print Hollow Mirrored Right Triangle Star Pattern

In this article we are going to see how to print the Hollow and Mirrored Right Angle Star Pattern

Example-1

For rows = 5
      *
     **
   *  *
 *    *
*****
Example-2

For rows = 6
         *
       **
     *  *
   *    *
 *      *
******

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

Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.

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 star.
  • After every iteration print a new line.

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 input from user
        int  rows=scan.nextInt();
        //Outer loop
        for (int r=1; r<=rows; r++)
        {//Inner loop
            // Print space in decreasing order
            for (int c=rows; c>r; c--)
            {
                System.out.print(" ");
            }
            // Print stars in increasing order
            for (int k=1; k<=r; k++)
            {
                if( k == 1 || k == r || r == rows)
                    System.out.print("*");
                else
                    System.out.print(" ");
            }
            //Prints a newline
            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 rows input from user
        int  rows=scan.nextInt();
        System.out.print("Symbol : ");
        //Taking symbol input from user
        char s=scan.next().charAt(0);
        
        //Outer loop
        for (int r=1; r<=rows; r++)
        {//Inner loop
            // Print space in decreasing order
            for (int c=rows; c>r; c--)
            {
                System.out.print(" ");
            }
            // Print stars in increasing order
            for (int k=1; k<=r; k++)
            {
                if( k == 1 || k == r || r == rows)
                    System.out.print(s);
                else
                    System.out.print(" ");
            }
            //Prints a newline
            System.out.println();
        }  
    }  
}


Output:

Rows : 5
Symbol : $
        $
     $ $
   $   $
  $    $
$$$$$

Explanation :

Let’s understand the program by going through the detailed explanation.

We have taken rows value as 5.

Iteration-1

r=1, goes in the first inner loop and prints four space as c > r.

It prints 1 star as k == 1

    *

Iteration-2

r=2, goes in the first inner loop and prints three space as c > r.

It prints two star as k == 1 and k == r

   **

Iteration-3

r=3, goes in the first inner loop and prints two space as c > r.

It prints two stars as k ==1 and k == r and rest space.

  *  *

Iteration-4

r=4, goes in the first inner loop and prints one space as c > r.

It prints two stars as k == 1 and k == r and rest space.

 *    *

Iteration-5

r=5, goes out the first inner loop. For the next loop it prints 5 stars as r == rows

*****

After this r=6 i.e.  so program will come out of the loop.

Now, after end of all iteration we will see the complete pattern is printed on the output screen like this.

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

C Code:

#include <stdio.h>
int main(int argc, char const *argv[])
{
    printf("Enter rows : ");
    int rows;
    //Taking row as input from user
    scanf("%d", &rows);
    //Outer loop
    for (int r = 1; r <= rows; r++)
    { //Inner loop
        // Print space in decreasing order
        for (int c = rows; c > r; c--)
        {
            printf(" ");
        } //Inner loop 2
        // Print stars in increasing order
        for (int k = 1; k <= r; k++)
        {
            if (k == 1 || k == r || r == rows)
            {
                printf("*");
            }
            else
            {
                printf(" ");
            }
        } //To print a newline
        printf("\n");
    }
    return 0;
}

Output:

Enter rows : 5
         *
       **
     *  *
   *    *
  *****

C++ Code:

#include <iostream>
using namespace std;
int main(int argc, char const *argv[])
{
    cout << "Enter rows : ";
    int rows;
    //Taking row as input from user
    cin >> rows;
    //Outer loop
    for (int r = 1; r <= rows; r++)
    { //Inner loop
        // Print space in decreasing order
        for (int c = rows; c > r; c--)
        {
            cout << " ";
        }
        // Print stars in increasing order
        for (int k = 1; k <= r; k++)
        {
            if (k == 1 || k == r || r == rows)
                cout << "*";
            else
                cout << " ";
        } //To print a newline
        cout << endl;
    }
    return 0;
}
Output:

Enter rows : 5
         *
       **
     *  *
   *    *
  *****

Related Java Star Pattern Programs: