Technology
Printing Square Patterns in Java: Techniques and Examples
Printing Square Patterns in Java: Techniques and Examples
In Java, printing square patterns using nested loops is a common exercise for beginners and a fundamental concept for understanding loop operations. This tutorial will explore various methods to print square patterns, including asterisk patterns and filled and hollow squares. Let's dive into the code examples and explanations.
Printing a Simple Square Pattern
Code Example
The following is an example of a simple Java program that prints a square pattern of asterisks (#42;).
import ;
public class SquarePattern {
public static void main(String[] args) {
int size 5; // You can change this value to increase or decrease the size of the square
for (int i 0; i size; i ) { // Loop for each row
for (int j 0; j size; j ) { // Loop for each column
(* );
}
(); // Move to the next line after each row
}
}
}
Explanation:
Outer Loop: This loop runs size times, representing each row of the square. Inner Loop: This loop runs size times, representing each column in a row. (* ); Prints an asterisk followed by a space without moving to a new line. (); Moves the cursor to the next line after finishing one row.If size is set to 5, the output will be:
*****
*****
*****
*****
*****
You can modify the size variable to print squares of different dimensions.
Adapting the Code for Input from the Keyboard
Below is an example where the size of the square is determined through user input. Additionally, it demonstrates two different patterns: a filled square and a hollow square.
Code Example
import ;
public class Samarth {
public static void main(String arg[]) {
Scanner sc new Scanner();
int n ();
int m ();
for (int line 1; line m; line ) {
for (int i 1; i n; i ) {
if (line 1 || line m || i 1 || i n) {
("*");
} else {
( );
}
}
();
}
}
}
Explanation:
The Scanner class is used to take input from the keyboard. The outer loop runs m times, representing each row. The inner loop runs n times, representing each column. The if condition filters the edges of the square to print asterisks.Compact Version of Square Patterns
For a more compact version, you can define separate functions for filled and hollow squares. Here is an example of how to achieve this:
Function Definitions
import ;
public class Patterns {
public static void filledSquare(int number) {
for (int i 0; i number; i ) {
for (int j 0; j number; j ) {
("*");
}
();
}
}
public static void hollowSquare(int number) {
for (int i 1; i number; i ) {
for (int j 1; j number; j ) {
if (i 1 || i number || j 1 || j number) {
("*");
} else {
( );
}
}
();
}
}
public static void main(String[] args) {
Scanner scan new Scanner();
int number ();
filledSquare(number);
();
hollowSquare(number);
}
}
Explanation:
filledSquare(number): Prints a filled square pattern of the given size. hollowSquare(number): Prints a hollow square pattern of the given size.Conclusion
Printing square patterns in Java is a great way to understand nested loops and basic programming concepts. By exploring the different patterns and input methods, you can gain valuable experience in Java programming and enhance your coding skills.
Keywords
Java Patterns, Nested Loops, Square Pattern Printing
-
Navigating the Challenges of Dynamic Programming: How to Solve Problems Comfortably
Navigating the Challenges of Dynamic Programming: How to Solve Problems Comforta
-
Quantifying Steel Requirements for a 1 Cubic Foot RCC Slab
Quantifying Steel Requirements for a 1 Cubic Foot RCC Slab Accurate construction