Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c
Program to print solid square star pattern in Java

Program to print solid square star pattern in Java

Posted on November 24, 2019November 24, 2019

Table of Contents

  • Program to print solid square star pattern in Java
    • Program to display  square star pattern
      • Program to print solid square Using for loop
      • Program to print solid square Using while loop
      • Program to print solid square Using the do-while loop

Program to print solid square star pattern in Java

In this article, we will discuss the concept of  Program to Print solid rectangle or square star pattern in Java programming language

C Program to print solid square star pattern
Solid square pattern

In this post, we are going to learn How to write a program to print  solid square star pattern in Java language using for loop, while loop and Do-while loop 

Program to display  square star pattern

Program 1

Program to print solid square Using for loop

This program allows the user  to enter the size and then it will display solid square pattern using for loop in  Java programming language

import java.util.Scanner;
class Solid_Rectangle{
public static void main(String args[]){
  
  Scanner scan=new Scanner(System.in); 
int size,i,j;//declare variables size,i,j;
  //create a scanner object for input
System.out.print("Please enter the size: ");
size=scan.nextInt();
//get input from the user for size

for (i=1; i<=size; i++){
  for (j=1; j<=size; j++){
  System.out.print("*");
  }
  System.out.print("\n");
}
}
}

When the above code is executed it produces the following output

Program to print solid square star pattern in Java
Output

Approach

  • This program requests the input for size
  • The input(size) is stored in the variable “size“
  • To iterate through the row, run through outer for loop from 1 to given size according to the loop structure  for(i=1; i<=size; i++)
  • To iterate through the column, run through inner for loop from 1 to given size according to the loop structure for(j=1; j<=size; j++) ;
  • inside inner loop print star “*”;
  • This activity continues until the condition of outer while loop becomes false.

Program to print solid square Using while loop

Program 2

This program allows the user  to enter the size and then it will display solid square pattern using while loop in  Java programming language

import java.util.Scanner;
class Solid_Rectangle1{
public static void main(String args[]){
  
  Scanner scan=new Scanner(System.in); 
int size,i,j;//declare variables size,i,j;
  //create a scanner object for input
System.out.print("Please enter the size: ");
size=scan.nextInt();
//get input from the user for size
i=1; ;
while(i<=size){
  j=1; 
  while(j<=size){
  System.out.print("*");
  j++;
  }
  System.out.print("\n");
   i++;
}
}
}

When the above code is executed it produces the following output

Program to print solid square star pattern in Java
Output

Approach

  • The program requests to input for the “size of the pattern”
  • The input stores in the variable “size”
  • To iterate through the row, run the outer while loop from 1 to given size according to the loop structure  while(i<=size)
  • To iterate through the column, run the inner while loop from 1 to given size according to the loop structure  while(j<=size);
  • inside inner loop print “*”
  • This activity continues until the condition of outer while loop becomes false

Program to print solid square Using the do-while loop

Program 3

This program allows the user  to enter the size and then it will display solid square pattern using the do-while loop in  Java programming language

import java.util.Scanner;
class Solid_Rectangle2{
public static void main(String args[]){
  
  Scanner scan=new Scanner(System.in); 
int size,i,j;//declare variables size,i,j;
  //create a scanner object for input
System.out.print("Please enter the size: ");
size=scan.nextInt();
//get input from the user for size
i=1; ;
do{
  j=1; 
  do{
  System.out.print("*");
  j++;
  }while(j<=size);
  System.out.print("\n");
   i++;
}while(i<=size);
}
}

When the above code is executed it produces the following output

Program to print solid square star pattern in Java
Output

Approach

  • The program requests to input for the “size of the pattern”
  • The input stores in the variable “size”
  • To iterate through the row, run the outer do-while loop from 1 to given size according to the loop structure  while(i<=size);
  • To iterate through the column, run the inner do-while loop from 1 to given size according to the loop structure  while(j<=size);
  • inside inner loop print star
  • This activity continues until the condition of outer while loop becomes false

Suggested for you

for loop in Java language

Nested for loop in Java language

while loop in Java language

Nested while loop in Java language

Do-while loop in Java language

 

Similar post

Program to print hollow square star pattern in Java

Program to print hollow square star pattern in C

Program to print hollow square star pattern in C++

C Program to print solid square star pattern

Java Program to print solid square star pattern

C++ Program to print solid square star pattern

 

 

 

 

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes