Java Program to Print Square Pattern Star Pattern
Tags: java, programming
Published: 2021-07-08 12:01:19

Program to Print Square Star Pattern</h2>
In this article we are going to see how to print the square  star program.
<pre>Example-1

When row value=4
****
****
****
****</pre>
<pre>Example-2:

When row value=5
*****
*****
*****
*****
*****</pre>
Now, let's see the actual program to print it.

<strong>Approach:</strong>
<ul>
 	<li>Enter total row and store it in an integer variable <code>row</code>.</li>
 	<li>Take first for loop to print all rows.</li>
 	<li>Take second/inner for loop to print column values.</li>
 	<li>Then go on printing the star symbols according to the iteration.</li>
</ul>
<strong>JAVA Code:</strong>
<ul>
 	<li><a href="#Static_Star_Character">Static Star Character</a></li>
 	<li><a href="#User_Input_Character">User Input Character</a></li>
</ul>
<h3><a id="Static_Star_Character"></a>Method-1 : Static Star Pattern</h3>
<pre class="EnlighterJSRAW" data-enlighter-language="java">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();
    //for loop for rows
    for(  r=0;r&lt;row;r++)
        {
            // printing stars
            for( c=0;c&lt;row;c++)
                 System.out.print("*");
            System.out.println();
       }             
    }
}

</pre>
<pre>Output:

Enter rows : 5
*****
*****
*****
*****
*****</pre>

<ul><li><a href="https://btechgeeks.com/java-program-to-print-pyramid-star-pattern/">Java Program to Print Pyramid Star Pattern</a></li>
<li><a href="https://btechgeeks.com/java-program-to-print-inverted-pyramid-star-pattern/">Java Program to Print Inverted Pyramid Star Pattern</a></li></ul>

<h3><a id="User_Input_Character"></a>Method-2 : User Input Pattern</h3>
<pre class="EnlighterJSRAW" data-enlighter-language="java">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();
    // entering any character
    System.out.print("Enter any character : ");
    char sym = s.next().charAt(0);
    //for loop for rows
    for(  r=0;r&lt;row;r++)
        {
            // printing stars
            for( c=0;c&lt;row;c++)
                 System.out.print(sym);
            System.out.println();
       }             
    }
}

</pre>
<pre>Output:

Enter rows : 5
Enter any character : #
#####
#####
#####
#####
#####</pre>
<strong>Explanation :</strong>

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

We have taken row value as 5.

<strong>Iteration-1</strong>
r=0 (passes the first for loop condition) because it will execute until <code>r&lt; row</code>
Star will be printed 5 time because inner for loop will be executed <code>row</code> time .
<pre>*****</pre>
<strong>Iteration-2</strong>
r=1 (passes the first for loop condition) because it will execute until <code>r&lt; row</code>
Star will be printed 5 time because inner for loop will be executed <code>row</code> time .
<pre>*****</pre>
<strong>Iteration-3</strong>
r=2 (passes the first for loop condition) because it will execute until <code>r&lt; row</code>
Star will be printed 5 time because inner for loop will be executed <code>row</code> time .
<pre>*****</pre>
<strong>Iteration-4</strong>

r=3 (passes the first for loop condition) because it will execute until <code>r&lt; row</code>
Star will be printed 5 time because inner for loop will be executed <code>row</code> time .
<pre>*****</pre>
<strong>Iteration-5</strong>

r=4 (passes the first for loop condition) because it will execute until <code>r&lt; row</code>
Star will be printed 5 time because inner for loop will be executed <code>row</code> time .
<pre>*****</pre>
Now i=5, so first for loop condition will fail.  So next for loop will not be executed any more.

Now, after end of all iteration we will see the complete pattern is printed on the output screen like this.
<pre>*****
*****
*****
*****
*****</pre>
<strong>C Code:</strong>
<pre class="EnlighterJSRAW" data-enlighter-language="c">#include &lt;stdio.h&gt;
int main() {
   int r, row, c ,d;
   printf("Enter rows: ");
   scanf("%d", &amp;row);
  for(  r=0;r&lt;row;r++)
        {
            // printing stars
            for( c=0;c&lt;row;c++)
                printf("*");
           printf("\n");
       } 
   return 0;
}

</pre>
<pre>Output:

Enter rows : 5
*****
*****
*****
*****
*****</pre>
<strong>C++ Code:</strong>
<pre class="EnlighterJSRAW" data-enlighter-language="cpp">#include &lt;iostream&gt;
using namespace std;
int main()
{
   int row, r , c ,d ;
   cout &lt;&lt; "Enter  rows: ";
   cin &gt;&gt; row;
  for(  r=0;r&lt;row;r++)
        {
            // printing stars
            for( c=0;c&lt;row;c++)
                cout&lt;&lt; "*" ;
          cout &lt;&lt; "\n" ;
       } 
    return 0;   
}
</pre>
<pre>Output:

Enter rows : 5
*****
*****
*****
*****
*****</pre>