In this article, we will discuss the concept of Program to Display solid rectangle or square star pattern in the Java programming language
In this post, we are going to learn how to write a program to print solid rectangle or square star pattern in Java using for loop, while loop and Do-while loop with example programs
Program 1
This program allows the user to enter the number of rows and columns and then it will display the solid rectangle or square pattern using for loop in Java programming language
import java.util.Scanner; class RectanglePat1{ public static void main(String args[]){ int rows,columns;//declare variable size Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.print("Enter the nunber of rows: "); rows=scan.nextInt(); //get input from the user for row System.out.print("Enter the nunber of rows: "); columns=scan.nextInt(); //get input from the user for coloum for (int i=1; i<=rows; i++){ for (int j=1; j<=columns; j++){ System.out.print("*"); } System.out.print("\n"); } } }
When the above code is executed, it produces the following results
Program 2
This program allows the user to enter the number of rows and columns and then it will display the solid rectangle or square pattern using while loop in Java programming language
import java.util.Scanner; class RectanglePat2{ public static void main(String args[]){ int rows,columns;//declare variable size Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.print("Enter the nunber of rows: "); rows=scan.nextInt(); //get input from the user for row System.out.print("Enter the nunber of rows: "); columns=scan.nextInt(); //get input from the user for coloum int i=1; while( i<=rows){ int j=1; while(j<=columns){ System.out.print("*"); j++; } System.out.print("\n"); i++; } } }
When the above code is executed, it produces the following results
Program 3
This program allows the user to enter the number of rows and columns and then it will display the solid rectangle and square pattern using the do-while loop in Java programming language
import java.util.Scanner; class RectanglePat3{ public static void main(String args[]){ int rows,columns;//declare variable size Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.print("Enter the nunber of rows: "); rows=scan.nextInt(); //get input from the user for row System.out.print("Enter the nunber of rows: "); columns=scan.nextInt(); //get input from the user for coloum int i=1; do{ int j=1; do{ System.out.print("*"); j++; }while(j<=columns); System.out.print("\n"); i++; }while( i<=rows); } }
When the above code is executed, it produces the following results
Suggested for you
Nested For loop in Java language
Nested while loop in Java language
Do-while loop in Java language
Nested Do-while loop in Java language
Datatype and variable in Java language
Similar post
C code to Print Hollow rectangle and square star pattern
C++ code to Print Hollow rectangle and square star pattern
Java code to Print Hollow rectangle and square star pattern
Print solid rectangle and square star pattern in C
Print solid rectangle and square star pattern in C++
How to find reverse number using method In this article, we will discuss the concept…
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…