Lesson Plan
Java Pattern-1
Pre-requisites:
Java Synta
Loops
List of patterns involved:
Solid Squar
Solid Rectangl
Number Squar
Star Triangl
Star Triangle Revers
Number Triangl
Odd Number Triangl
Alphabet Squar
Star Plu
Star Cros
Floyd’s Triangl
Binary Triangl
Star Triangle Flippe
Number Triangle Flipped
Q1) Print the given Pattern:
Solid Square
Java + DSA
public class Main {
public static void main(String[] args) {
int n = 4; // You can change this value to adjust
the size of the square
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print("* ");
System.out.println(); // Move to the next line
after each row
Q2) Print the given Pattern:
Solid Rectangle
public class Main {
public static void main(String[] args) {
int rows = 4; // You can change this value to adjust
the number of rows
for (int i = 0; i < rows; i++) {
for (int j = 0; j < 5; j++) {
System.out.print("* ");
System.out.println(); // Move to the next line
after each row
Java + DSA
Q3) Print the given Pattern:
Number Square
public class Main {
public static void main(String[] args) {
int rows = 4;
int columns = 4;
for (int i = 0; i < rows; i++) {
for (int j = 1; j <= columns; j++) {
System.out.print(j + " ");
System.out.println(); // Move to the next line
after each row
Q4) Print the given Pattern:
Star Triangle
Java + DSA
public class Main {
public static void main(String[] args) {
int rows = 4;
for (int i = 1; i <= rows; i++) {
// Print '*' i times in each row
for (int j = 1; j <= i; j++) {
System.out.print("* ");
System.out.println();
Q5) Print the given Pattern:
Star Triangle Reverse
Java + DSA
public class Main {
public static void main(String[] args) {
int rows = 4;
for (int i = rows; i >= 1; i--) {
// Print '*' i times in each row
for (int j = 1; j <= i; j++) {
System.out.print("* ");
System.out.println();
Q6) Print the given Pattern:
Number Triange
public class Main {
public static void main(String[] args) {
int rows = 4;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j + " ");
System.out.println();
Java + DSA
Q7) Print the given Pattern:
Odd Number Triangle
public class Main {
public static void main(String[] args) {
int rows = 4;
for (int i = 0; i < rows; i++) {
int num = 1;
for (int j = 0; j <= i; j++) {
System.out.print(num + " ");
num += 2;
System.out.println();
Q8) Print the given Pattern:
Alphabet Square
Java + DSA
public class Main {
public static void main(String[] args) {
int rows = 4;
for (int i = 0; i < rows; i++) {
for (char ch = 'A'; ch < 'A' + rows; ch++) {
System.out.print(ch + " ");
System.out.println();
Q9) Print the given Pattern:
Star Plus
Java + DSA
public class Main {
public static void main(String[] args) {
int rows = 5;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < rows; j++) {
if (i == 2 || j == 2) {
System.out.print("* ");
} else {
System.out.print(" ");
System.out.println();
Q10) Print the given Pattern:
Star Cross
public class Main {
public static void main(String[] args) {
int size = 5;
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (i == j || i + j == size - 1) {
System.out.print("* ");
} else {
System.out.print(" ");
Java + DSA
}
System.out.println();
Q11) Print the given Pattern:
Floyd’s Triangle
public class Main {
public static void main(String[] args) {
int rows = 4; // You can change this value to adjust
the number of rows
int number = 1;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(number + " ");
number++;
System.out.println();
Java + DSA
Q12) Print the given Pattern:
Binary Triangle
public class Main {
public static void main(String[] args) {
int rows = 4;
for (int i = 0; i < rows; i++) {
int value = (i+1) % 2; // Start with 1 for even
rows, 0 for odd rows
for (int j = 0; j <= i; j++) {
System.out.print(value + " ");
value = 1 - value; // Toggle between 0 and 1
System.out.println();
Q13) Print the given Pattern:
Star Triangle Flipped
Java + DSA
public class Main {
public static void main(String[] args) {
int rows = 4; // You can change this value to adjust
the number of rows
for (int i = rows - 1; i >= 0; i--) {
for (int j = 0; j < i; j++) {
System.out.print(" ");
for (int k = i; k < rows; k++) {
System.out.print("* ");
System.out.println();
Q14) Print the given Pattern:
Number Triangle Flipped
Java + DSA
public class Main {
public static void main(String[] args) {
int rows = 4; // You can change this value to adjust
the number of rows
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= rows - i; j++) {
System.out.print(" ");
for (int k = 1; k <= i; k++) {
System.out.print(k + " ");
System.out.println();
Upcoming Lecture:
More interesting patterns
Java + DSA