DEV Community

Cover image for Java Program to Print Square Pattern Star Pattern
Sachin Ponnapalli
Sachin Ponnapalli

Posted on

Java Program to Print Square Pattern Star Pattern

Program to Print Square Star Pattern
In this article we are going to see how to print the square  star program.

Example-1

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

When row value=5
*****
*****
*****
*****
*****
Now, let's see the actual program to print it. Approach:
  • Enter total row and store it in an integer variable row.
  • Take first for loop to print all rows.
  • Take second/inner for loop to print column values.
  • Then go on printing the star symbols according to the iteration.
JAVA Code:

Method-1 : Static Star Pattern

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<row;r++)
        {
            // printing stars
            for( c=0;c<row;c++)
                 System.out.print("*");
            System.out.println();
       }             
    }
}

Output:

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

Method-2 : User Input Pattern

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<row;r++)
        {
            // printing stars
            for( c=0;c<row;c++)
                 System.out.print(sym);
            System.out.println();
       }             
    }
}

Output:

Enter rows : 5
Enter any character : #
#####
#####
#####
#####
#####
Explanation : Let's understand the program by going through the detailed explanation. We have taken row value as 5. Iteration-1 r=0 (passes the first for loop condition) because it will execute until r< row Star will be printed 5 time because inner for loop will be executed row time .
*****
Iteration-2 r=1 (passes the first for loop condition) because it will execute until r< row Star will be printed 5 time because inner for loop will be executed row time .
*****
Iteration-3 r=2 (passes the first for loop condition) because it will execute until r< row Star will be printed 5 time because inner for loop will be executed row time .
*****
Iteration-4 r=3 (passes the first for loop condition) because it will execute until r< row Star will be printed 5 time because inner for loop will be executed row time .
*****
Iteration-5 r=4 (passes the first for loop condition) because it will execute until r< row Star will be printed 5 time because inner for loop will be executed row time .
*****
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.
*****
*****
*****
*****
*****
C Code:
#include <stdio.h>
int main() {
   int r, row, c ,d;
   printf("Enter rows: ");
   scanf("%d", &row);
  for(  r=0;r<row;r++)
        {
            // printing stars
            for( c=0;c<row;c++)
                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=0;r<row;r++)
        {
            // printing stars
            for( c=0;c<row;c++)
                cout<< "*" ;
          cout << "\n" ;
       } 
    return 0;   
}
Output:

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

Top comments (0)