Solid rectangle pattern
Table of Contents
In this article, we will discuss the concept of Program to Print solid rectangle or square star pattern in Java programming language
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 1
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
Approach
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
Approach
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
Approach
Suggested for you
Nested for 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
Multiply two numbers in Java using scanner| 5 different ways In this article, we will…
5 Different ways to Divide two numbers in Java using scanner In this article, we…
Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…
10 ways to subtract two numbers in Java In this article, we will discuss the…
Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…
How to Divide two numbers in Java| 5 different ways In this article, we will…
This website uses cookies.