Java Program to Print Solid Square Inside A Square Star Pattern

Program to Print Solid Square Inside A Square Star Pattern

In this article we are going to see how to print Solid Square Inside a square  star program.

Example-1

When row value=10

**********
*           *
* ****** *
* ****** *
* ****** *
* ****** *
* ****** *
* ****** *
*           *
**********
Example-2:

When row value=7

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

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 total row and store it in an integer variable row.
  • Take first for loop to print the row value and a star for each row.
  • Take first inner for loop to print the column value i.e., stars according to the condition
    if ((r == 1 || r == row) || (c == 1 || c == row) || (r >= 3 && r <= row - 2) && (c >= 3 && c <= row - 2))  else it will print spaces .
  • Then go on printing the star symbol according to loop.

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;
    //creating object 
    Scanner s = new Scanner(System.in);
    // entering the number of row
    System.out.print("Enter rows : ");
    row = s.nextInt();
    //outer for loop 
    for (r = 1; r <= row; r++)
        {
            for ( c = 1; c <= row; c++)
            {
                // condition for printing stars
                 if ((r == 1 || r == row) || (c == 1 || c == row) || (r >= 3 && r <= row - 2) && (c >= 3 && c <= row - 2))
                     System.out.print("*");
                 else
                    System.out.print(" ");
            }
            System.out.println();
        }
  }
}
Output :

Enter row :  7

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

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;
    //creating object 
    Scanner s = new Scanner(System.in);
    // entering the number of row
    System.out.print("Enter rows : ");
    row = s.nextInt();
    // entering any character
    System.out.print("Enter character : ");
    char square = s.next().charAt(0);
    //outer for loop 
    for (r = 1; r <= row; r++)
        {
            for ( c = 1; c <= row; c++)
            {
                // condition for printing stars
                 if ((r == 1 || r == row) || (c == 1 || c == row) || (r >= 3 && r <= row - 2) && (c >= 3 && c <= row - 2))
                     System.out.print(square);
                 else
                    System.out.print(" ");
            }
            System.out.println();
        }
  }
}
Output :

Enter row :  7
Enter character : *

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

C Code:

#include <stdio.h>
int main() 
{
   int r, row, c;
   printf("Enter rows: ");
   scanf("%d", &row);
   for (r = 1; r <= row; r++)
        {
            for ( c = 1; c <= row; c++)
            {
                 if ((r == 1 || r == row) || (c == 1 || c == row) || (r >= 3 && r <= row - 2) && (c >= 3 && c <= row - 2))
                      printf("*");
                 else
                    printf(" ");
            }
             printf("\n");
        }
   return 0;
}
 
Output :

Enter row :  7

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

C++ Code:

#include <iostream>
using namespace std;
int main()
{
   int row, r , c ;
   cout << "Enter  rows: ";
   cin >> row;
   for (r = 1; r <= row; r++)
        {
            for ( c = 1; c <= row; c++)
            {
                 if ((r == 1 || r == row) || (c == 1 || c == row) || (r >= 3 && r <= row - 2) && (c >= 3 && c <= row - 2))
                     cout << "*";
                 else
                    cout << " ";
            }
            cout << "\n";
        }
   return 0;
}


Output :

Enter row :  7

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

Related Java Star Pattern Programs:

Leave a Comment