Java Program to Print Hollow Mirrored Rhombus Star Pattern

Program to Print Hollow Mirrored Rhombus Star Pattern

In this article we are going to see how to print the Hollow Mirrored rhombus star program.

Example-1

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

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

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

Beginners and experienced programmers can rely on these Best Java Programs Examples and code various basic and complex logics in the Java programming language with ease.

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 with the condition  if(r == 1 || r == row || c == 1 || c == row).

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++)
        {
            for (c = 1;   c<= r-1; c++)
                System.out.print(" ");
            // Print star in decreasing order
            for (c = 1; c <= row ; c++)
            {
                if( r == 1 || r == row || c == 1 || c == row  )
                    System.out.print("*");
                else
                    System.out.print(" ");
            }
            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 sym;
    //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 : ");
    sym = s.next().charAt(0);
    //printing row value 
   for (r = 1; r <= row ; r++)
        {
            for (c = 1;   c<= r-1; c++)
                System.out.print(" ");
            // Print star in decreasing order
            for (c = 1; c <= row ; c++)
            {
                if( r == 1 || r == row || c == 1 || c == row  )
                    System.out.print(sym);
                else
                    System.out.print(" ");
            }
            System.out.println();
        }
    }
}
Output :

Enter Rows :5
Enter character : #
#####
   #      #
     #       #
         #      #
            #####

C Code:

#include <stdio.h>
int main() {
   int r, row, c ,d;
   printf("Enter rows: ");
   scanf("%d", &row);
  for (r = 1; r <= row ; r++)
        {
            for (c = 1;   c<= r-1; c++)
                printf(" ");
            // Print star in decreasing order
            for (c = 1; c <= row ; c++)
            {
                if( r == 1 || r == row || c == 1 || c == row  )
                    printf("*");
                else
                     printf(" ");
            }
             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++)
        {
            for (c = 1;   c<= r-1; c++)
               cout << " ";
            // Print star in decreasing order
            for (c = 1; c <= row ; c++)
            {
                if( r == 1 || r == row || c == 1 || c == row  )
                   cout << "*";
                else
                   cout << " ";
            }
            cout << "\n";
        }
    return 0;
}

Output :

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

Related Java Star Pattern Programs: