Java Program to Print Mirrored Rhombus Star Pattern

Program to Print Mirrored Rhombus Star Pattern

In this article we are going to see how to print mirrored rhombus star program.

Example-1

When row value=4
****
  ****
    ****
      ****
Example-2:

When row value=5
*****
  *****
    *****
      *****
        *****

Now, let’s see program to  how print it.

Want to excel in java coding? Practice with these Java Programs examples with output and write any kind of easy or difficult programs in the java language

Approach:

  • Enter total row and store it in an integer variable row.
  • Take first outer for loop to print all rows.
  • Take  first inner for loop to print column values i.e., first inner for loop will print all the spaces  in the column.
  • Take second  inner for loop to print column values i.e., second inner for loop will print all the stars in the column.

JAVA Code:

Method-1 : Static Star Character

import java.util.*;
public class Main 
{    
    public static void main(String args[])   
    {   
    // taking variable for loop iteration and row .
    int row,r,c,d;
    //creating object 
    Scanner s = new Scanner(System.in);
    // entering the number of row
    System.out.print("Enter rows : ");
    row = s.nextInt();
    //printing row value 
    for (r = 1; r <= row ; r++)
        {
            //printing spaces
            for (c = 1; c <= r-1; c++)
                System.out.print(" ");
            // Print star in decreasing order
            for (c = 1; c <= row ; c++)
                System.out.print("*");
            //taking to the new line 
            System.out.println();
        }
    }    
}
Output:

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

Method-2 : User Input Character

import java.util.*;
public class Main 
{    
    public static void main(String args[])   
    {   
    // taking variable for loop iteration and row .
    int row,r,c,d;
    char op;
    //creating object 
    Scanner s = new Scanner(System.in);
    // entering the number of row
    System.out.print("Enter rows : ");
    row = s.nextInt();
    // entering any random character
    System.out.print("Enter character : ");
    op = s.next().charAt(0);
    //printing row value 
    for (r = 1; r <= row ; r++)
        {
            //printing spaces
            for (c = 1; c <= r-1; c++)
                System.out.print(" ");
            // Print star in decreasing order
            for (c = 1; c <= row ; c++)
                System.out.print(op);
            //taking to the new line 
            System.out.println();
        }
    }    
    
}
Output:

Enter Rows : 5
Enter character : $
$$$$$
  $$$$$
    $$$$$
      $$$$$
        $$$$$

Explanation :

Iteration-1

r=1 (passes the first for loop condition) as it will iterate up to r<=row times.

First inner for loop will print the space 1 to r-1  time that means 0 times.

then 2nd inner for loop will print the “* ”   row time , that means 5 time .

So, star will be printed five time.

*****

Iteration-2

r=2 (passes the first for loop condition) as it will iterate up to r<=row times.

First inner for loop will print the space 1 to r-1  time that means 1 times.

then 2nd inner for loop will print the “* ”   row time , that means 5 time .

So, star will be printed five time.

   *****

Iteration-3

r=3 (passes the first for loop condition) as it will iterate up to r<=row times.

First inner for loop will print the space 1 to r-1  time that means 2 times.

then 2nd inner for loop will print the “* ”   row time , that means 5 time .

So, star will be printed five time.

       *****

Iteration-4

r=4 (passes the first for loop condition) as it will iterate up to r<=row times.

First inner for loop will print the space 1 to r-1  time that means 3 times.

then 2nd inner for loop will print the “* ”   row time , that means 5 time .

So, star will be printed five time.

           *****

Iteration-5

r=5 (passes the first for loop condition) as it will iterate up to r<=row times.

First inner for loop will print the space 1 to r-1  time that means 4 times.

then 2nd inner for loop will print the “* ”   row time , that means 5 time .

So, star will be printed five time.

             *****

Now when r=6, first for loop condition will fail so no other loops will be executed.

At last, we will see a pattern like this

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

C Code:

#include <stdio.h>
int main() {
   int r, row, c ,d;
   printf("Enter rows: ");
   scanf("%d", &row);
   for (r = 1; r <= row ; r++)
        {
            //printing spaces
            for (c = 1; c <= r-1; c++)
                printf(" ");
            // Print star in decreasing order
            for (c = 1; c <= row ; c++)
                printf("*");
            //taking to the new line 
           printf("\n");
        }
   return 0;
}

Output:

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

C++ Code:

#include <iostream>
using namespace std;
int main()
{
   int row, r , c ,d ;
   cout << "Enter  rows: ";
   cin >> row;
    for (r = 1; r <= row ; r++)
        {
            //printing spaces
            for (c = 1; c <= r-1; c++)
                cout<< " ";
            // Print star in decreasing order
            for (c = 1; c <= row ; c++)
               cout<<"*";
            //taking to the new line 
           cout<<"\n";
        }
    return 0;
}
Output:

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

Related Java Star Pattern Programs: