In order to enhance your logical skills and the idea of loops, you ought to explore pattern printing in java. From simple to advanced patterns, all have their logically defined ways.
Before we venture into printing several patterns in java, let us first explore one trick in developing the logic for a certain pattern.
Let's say you have been given this kind of a pattern, and told to provide its logic in java.
The pattern is a simple right-angled triangle. Now, let's see how we can derive the logic step-wise:
Step 1:
Draw horizontal and vertical lines to get a grid-like box layout as shown:
Now we have 4 rows and columns, each row and column at a certain index i and j respectively.
Step 2:
Writing the code will be much easier since we have the complete logic. Finally, let's write the code in Java to match the pattern's logic.
public class rightAngledTriangle{
// main method
public static void main(String[] args){
// initialize no. of rows/lines
int n = 4;
// outer loop for the change in row/line
for (int i = 1; i <= n; i++){
// inner loop for printing the stars
for (int j = 1; j <= i; j++){
// print the stars without change in line
System.out.print("*");
}
// change the row/line
System.out.println();
}
}
}
Output:
*
* *
* * *
* * * *
* * * * *
With that in mind, we can start writing code for different types of patterns all the way from the simplest to more advanced. There are several tricks for each pattern and you can take your time to research and explore.
Let us start with the simple patterns:
public class Patterns{
public static void main(String[] args){
int n = 8;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
System.out.print("* ");
}
System.out.println();
}
}
}
Output:
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
public class Patterns{
public static void main(String[] args){
int n = 8;
// outer loop handles rows
for (int i = 0; i < n; i++) {
// inner loop to print spaces.
for (int j = n - i; j > 1; j--) {
System.out.print(" ");
}
// inner loop to print stars.
for (int j = 0; j <= i; j++) {
System.out.print("* ");
}
// printing new line for each row
System.out.println();
}
}
}
Output:
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
public class Patterns{
public static void main(String[] args){
int n = 8;
// outer loop to handle rows
for (int i = 1; i <= n; i++) {
// inner loop to handle columns
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
// print new line for each row
System.out.println();
}
}
}
Output:
*
**
***
****
*****
******
*******
********
public class Patterns{
public static void main(String[] args){
int n = 8;
// outer loop to handle rows
for (int i = n; i >= 1; i--) {
// inner loop to handle columns
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
// print new line for each row
System.out.println();
}
}
}
Output:
********
*******
******
*****
****
***
**
*
public class Patterns{
public static void main(String[] args){
int n = 8;
// outer loop to handle rows
for (int i = n; i >= 1; i--) {
// inner loop to print spaces.
for (int j = 1; j < i; j++) {
System.out.print(" ");
}
// inner loop to print stars.
for (int j = 0; j <= n - i; j++) {
System.out.print("*");
}
// print new line for each row
System.out.println();
}
}
}
Output:
*
**
***
****
*****
******
*******
********
public class Patterns{
public static void main(String[] args){
int n = 8;
// calculating number of spaces
int space = 2 * n - 2;
// outer loop to handle rows
for (int i = n; i > 0; i--) {
// inner loop to print spaces.
for (int j = 0; j < n - i; j++) {
System.out.print(" ");
}
// Decrement value of num after each loop
space -= 2;
// inner loop to print stars.
for (int j = 0; j < i; j++) {
System.out.print("*");
}
// print new line for each row
System.out.println();
}
}
}
Output:
********
*******
******
*****
****
***
**
*
public class Patterns{
public static void main(String[] args){
int n = 8;
// outer loop to handle rows
for (int i = 1; i <= n; i++) {
// inner loop to print spaces.
for (int j = i; j < n; j++) {
System.out.print(" ");
}
for (int k = 1; k <= (2 * i - 1); k++) {
// print stars.
if (k == 1 || i == n || k == (2 * i - 1)) {
System.out.print("*");
}
// print spaces
else {
System.out.print(" ");
}
}
System.out.println("");
}
}
}
Output:
*
* *
* *
* *
* *
* *
* *
***************
public class Patterns{
public static void main(String[] args){
int n = 8;
// outer loop to handle rows
for (int i = n; i >= 1; i--) {
// inner loop to print spaces.
for (int j = i; j < n; j++) {
System.out.print(" ");
}
for (int k = 1; k <= (2 * i - 1); k++) {
// print stars.
if (k == 1 || i == n || k == (2 * i - 1)) {
System.out.print("*");
}
// print spaces.
else {
System.out.print(" ");
}
}
System.out.println("");
}
}
}
Output:
***************
* *
* *
* *
* *
* *
* *
*
public class Patterns{
public static void main(String[] args){
int n = 8;
// upper triangle
// outer loop to handle rows
for (int i = n; i >= 1; i--) {
// inner loop to handle columns
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
// printing new line for each row
System.out.println();
}
// lower triangle
// outer loop to handle rows
for (int i = 2; i <= n; i++) {
// inner loop to handle columns
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
// printing new line for each row
System.out.println();
}
}
}
Output:
********
*******
******
*****
****
***
**
*
**
***
****
*****
******
*******
********
More patterns coming up! ✌️