Square Star and Rhombus Star Pattern
Reminded of your geometry classes in your school by reading the title? If you are not a maths lover, don't be afraid by reading the title. You just need to know what a square and rhombus is. I would strongly suggest doing these questions as it will enhance your logical thinking capabilities and you will enjoy doing these questions. These questions are more like playing games (I am not exaggerating ) πβοΈ.
Developing the attitude and basic logical aptitude for problem solving really helps in the long run.
First, we will do square pattern questions then rhombus pattern. Also, we will be using loop concepts(such as nesting of loops) in these questions, I assume that you have gone through those articles in the Control Flow section. (Please don't tell me assumptions lead to disappointment π₯Ή).
Suggestion : Dry running examples helps a lot in these type of problems. You will catch the error instantly if you dry run and you can even understand the solution in a much better way.
Problem 1: Square Star Pattern
Write a java program to print a square star(*) pattern series of N rows.
Input
Input number of rows: 5
Output
*****
*****
*****
*****
*****
Approach
Look at the pattern for some time. What is coming to your mind??
Intuition
There are 5 rows and 5 columns. Each row and column has 5 stars. What if I say recreate this pattern in a Word document? A lazy person like me would type 5 stars like this "*****" and then copy this in other rows.
I am too lazy to even type these 5 stars for the first time in the first row. Instead, I will type one star and copy the rest even in the first row. You can do this using one for loop in C++ right? And then copying this "*****" in the subsequent 4 rows will recreate the pattern. Can't we do this other iteration of copy-pasting using another for loop? So we will need two for loops. Ask yourself will there be nesting of these two loops?? Yes, there will be nesting of two "for" loops.
Hint: Iterate through N rows, and for each row iterate for N columns.
Now give it a try !!
Explanation/Steps
The pattern is a matrix of N rows and columns containing stars(asterisks). Here, you need to iterate through N rows, and for each row iterate for N columns.
- Input number of rows from user. Store it in some variable say N.
- To iterate through rows, run an outer loop from 1 to N. The loop structure should be similar to for(i=1; i<=N; i++).
- To iterate through columns, run an inner loop from 1 to N. Define a loop inside above loop with structure for(j=1; j<=N; j++).
- Inside inner loop print *.
- After printing all columns of a row move to next line i.e. print a new line.
Solution
import java.util.*;
public class Main {
public static void main(String[] args) {
// Input number of rows from user
Scanner scanner = new Scanner(System.in);
System.out.println("Enter number of rows: ");
int N = scanner.nextInt(); //total rows
// Iterate through N rows
for (int i = 1; i <= N; i++) {
// Iterate over columns
for (int j = 1; j <= N; j++) {
// Print star for each column
System.out.print("*");
}
// Move to the next line/row
System.out.println();
}
}
}
Problem 2: Hollow Square Star Pattern
Write a java program to print a hollow square star(*) pattern series of N rows.
Input
Enter number of rows: 5
Output
*****
* *
* *
* *
*****
Approach
Notice the pattern. What do you think? Isn't it similar to the pattern of problem 1? This time we just have the border of the square. Help me in defining "border". This time we just have the first and last row and the first and the last column. Are we on the same page??
Intuition
The above pattern is similar to square star pattern of N rows and N columns. Here star is printed only for first and last column or for first and last row. If we relate to the first problem, we can say that we will need two for loops. One outer loop to iterate over each row and one inner loop to iterate over each column. But this time there will be some changes in inner loop as there are some conditions(star is printed only for first or last column or for first or last row). What do we use for conditions?? "if else ".We basically need to print two things "*"(asterisks) and " "(space). Think of conditions. When you will print what? Print star if row==1 or row==N or column==1 or column==N otherwise print space. Now give it a try !!
Explanation/Steps
- Input number of rows to print from user. Store it in a variable say N.
- To iterate through rows, run an outer loop from 1 to N. For that define loop with structure for(i=1; i<=N; i++).
- To iterate through columns, run an inner loop from 1 to N. Define loop with structure for(j=1; j<=N; j++).
- Inside inner loop print star for first and last row or for first and last column. Which is print star if i==1 or i==N or j==1 or j==N, otherwise print space.
- After printing all columns of a row, move to next line i.e. print a blank line after inner loop.
Solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// Input number of rows from user
System.out.println("Enter number of rows: ");
Scanner scanner = new Scanner(System.in);
int N = scanner.nextInt();// total rows
// Iterate through rows
for (int i = 1; i <= N; i++) {
// Iterate over columns
for (int j = 1; j <= N; j++) {
if (i == 1 || i == N || j == 1 || j == N) {
/* Print star for 1st and Nth row and column */
System.out.print("*");
} else {
System.out.print(" ");
}
}
// Move to the next line/row
System.out.println();
}
scanner.close();
}
}
Problem 3: Hollow Square Star Pattern with Diagonal
Write a java program to print a hollow square star pattern with diagonals.
Input
Enter number of rows: 5
Output
*****
** **
* * *
** **
*****
Approach
Notice the pattern and try to find the similarity between this pattern and the hollow square pattern. What do you think?? Isn't it similar to the pattern of problem 2? The above pattern is a simple hollow square star pattern if we remove the diagonal. This time we just have 2 extra diagonals. Are we on the same page??
Intuition
The above pattern is a simple hollow square star pattern(pattern of problem 2) with 2 diagonals diagonals. Think about how we can incorporate the diagonals. A star at the diagonal will follow either of two conditions: i)Row Number equals to Column Number OR ii) Column equals to N-i+1 (where i is current Row number). Why N-i+1 ??Dry run by substituting values of N and i and see yourself.
For each row stars are printed in below conditions:
- For first or last row.
- For first or last columns.
The above two conditions are same as Problem 2 (hollow square star pattern)
- Diagonal Condition: If row equals to column or column equals to N-i+1 (where i is current row number).
For the rest of the cases print " "(space).
Now give it a try !!
Explanation/Steps
- Input number of rows to print from user. Store it in some variable say N.
- To iterate through rows run an outer loop from 1 to N, increment loop counter by 1. The loop structure should look like for(i=1; i<=N; i++).
- To iterate through columns run an inner loop from 1 to N, increment loop counter by 1. The loop structure should look like for(j=1; j<=N; j++).
- Inside the loop print star for first or last row, first or last column or when row equals to column or column equals to N - i + 1 otherwise print space.
Solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// Input number of rows from the user
System.out.println("Enter number of rows: ");
Scanner scanner = new Scanner(System.in);
int N = scanner.nextInt(); //total rows
/* Iterate over each row */
for (int i = 1; i <= N; i++) {
/* Iterate over each column */
for (int j = 1; j <= N; j++) {
/*
* Print star for,
* first row (i==1) or
* last row (i==N) or
* first column (j==1) or
* last column (j==N) or
* row equal to column (i==j) or
* column equal to N-row (j==N-i+1)
*/
if (i == 1 || i == N || j == 1 || j == N || i == j || j == (N - i + 1)) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
/* Move to the next line */
System.out.println();
}
scanner.close();
}
}
Break time !! π₯³π₯³ We are done with square patterns. It was interesting, right?? I hope you enjoyed playing with these patterns.
Now we will start with Rhombus patterns.
Problem 4: Rhombus Star Pattern
Write a java program to print a rhombus star pattern of N rows.
Input
Enter number of rows: 5
Output
*****
*****
*****
*****
*****
Approach
Notice the pattern. Can we correlate this pattern with the pattern of problem 1?? I guess yes we can. The only difference which we can find is in terms of space. If we remove the starting spaces from every row, the pattern will become a square pattern.
Intuition
Have a close look at the pattern. Place your mouse cursor on to the pattern, to count spaces. Try to decode the logic of given pattern.
If we remove leading spaces the pattern becomes a simple square star pattern of N rows and columns. We only need to add logic of printing spaces with the existing logic of square star pattern. How many spaces are there in each row?? Try to figure that out as we will need to print that number of spaces first then stars.
For N=5 as given the output pattern
Row 1: 4 spaces // N-row number=?
Row 2: 3 spaces // N-row number=?
Row 3: 2 spaces // N-row number=?
Row 4: 1 spaces // N-row number=?
Row 5 : 0 spaces // N-row number=?
The pattern consists of N - i spaces per row (where i is the current row number).
Explanation/Steps
- Input number of rows from user. Store it in a variable say rows.
- To iterate through rows, run an outer loop from 1 to rows. Define a loop with structure for(i=1; i<=N; i++).
- To print spaces, run an inner loop from 1 to N - i. Why N - i ?? As we saw in the intuition section that the pattern consists N - i spaces per row (where N is total number of rows and i is the current row number). Construct a loop with structure for(j=1; j<=N - i; j++). Inside this loop print space.
- To iterate through columns for printing stars. Run another inner loop from 1 to rows. Define another loop with structure for(j=1; j<=N; j++). Inside this loop print star.
- After printing all columns of a row move to next line i.e. print new line.
Solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// Input number of rows from the user
System.out.println("Enter number of rows: ");
Scanner scanner = new Scanner(System.in);
int N = scanner.nextInt(); //total rows
for (int i = 1; i <= N; i++) {
/* Print leading spaces */
for (int j = 1; j <= N - i; j++) {
System.out.print(" ");
}
/* Print stars after spaces */
for (int j = 1; j <= N; j++) {
System.out.print("*");
}
/* Move to the next line */
System.out.println();
}
scanner.close();
}
}
Problem 5: Mirrored Rhombus Star Pattern
Write a java program to print the inverted/mirrored rhombus star pattern of N rows.
Input
Enter number of rows: 5
Output
*****
*****
*****
*****
*****
Approach
Have you gone through the 4th problem?? 5th problem and the 4th problem are brothers. π I mean patterns are mirror images of each other. Notice the pattern carefully. We just need to find the relationship between row number and number of spaces. Rest it is similar to 4th problem. Please give it a try guys without looking at the solution. I am very confident that you will be able to arrive at the solution if you have gone through the concept of above problems. π
Intuition
Have a close look at the pattern. Place your mouse cursor on to the pattern, to count spaces. Try to decode the logic of given pattern.
We only need to change the logic of printing spaces with the existing logic of rhombus star pattern(4th problem). How many spaces are there in each row?? Try to figure that out as we will need to print that number of spaces first then stars.
For N=5 as given the output pattern
Row 1 : 0 spaces // current row number-1=?(1-1=0)
Row 2: 1 spaces // current row number-1=?(2-1=1)
Row 3 : 2 spaces // current row number-1=?(3-1=2)
Row 4 : 3 spaces // current row number-1=?(4-1=3)
Row 5 : 4 spaces // current row number-1=?(5-1=4)
current row number-1 = Number of spaces in the current row
If you have noticed there are i - 1 spaces per row (where i is current row number).
Now give it a try !!
Explanation/Steps
- Input number of rows to print from user. Store it in a variable say N.
- To iterate through rows, run an outer loop from 1 to N, increment 1 in each iteration. The loop structure should look like for(i=1; i<=N; i++).
- To print spaces, run an inner loop from 1 to i - 1 with structure for(j=1; j<i; j++). Inside this loop print single blank space.
- To print stars, run another inner loop from 1 to N with structure for(j=1; j<=N; j++). Inside this loop print star.
- After printing all columns of a row, move to next line i.e. print new line.
Solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// Input number of rows from the user
System.out.println("Enter number of rows: ");
Scanner scanner = new Scanner(System.in);
int N = scanner.nextInt(); //total rows
for (int i = 1; i <= N; i++) {
/* Print leading spaces */
for (int j = 1; j < i; j++) {
System.out.print(" ");
}
for (int j = 1; j <= N; j++) {
System.out.print("*");
}
System.out.println();
}
scanner.close();
}
}
This was our last question for this exercise. Tired or enjoying?? Probably bothπ. Jokes apart, these types of problems help in developing the aptitude for problem solving and also help in logic building. As suggested at the beginning of the article, dry running with examples makes the code and logic more clear and you will retain it for a longer time.
"Remember Practice leads to Perfection".
Happy Learning !! πSee you in the next article with more interesting stuff.