Java Placement Course - Patterns and Nested Loops

Jul 29, 2024

Java Placement Course - Patterns and Nested Loops

Lecture Overview

  • Previous lecture covered loops: for, while, do while
  • Today’s lecture focuses on patterns.

What are Patterns?

  • Specific type of problems in coding.
  • Involve printing numbers or characters in a specific format on the screen.
  • Include basic to advanced questions, relevant for interviews.

Key Problems to Cover (9 Problems)

  1. Solid Rectangle
  2. Hollow Rectangle
  3. Half Pyramid
  4. Inverted Half Pyramid
  5. Inverted Half Pyramid with Spaces
  6. Floyd's Triangle
  7. 0-1 Triangle
  8. Additional variations and concepts regarding loops.

Problem 1: Solid Rectangle

  • Dimensions: 4 rows, 5 columns.
  • Logic: Use of nested loops.
    • Outer Loop: Controls the rows (i = 1 to 4).
    • Inner Loop: Controls the columns (j = 1 to 5).

Code Example:

int n = 4; // number of rows
int m = 5; // number of columns
for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
        System.out.print("*"); // printing star
    }
    System.out.println(); // move to next line
}

Problem 2: Hollow Rectangle

  • Logic: Only print stars on the boundary.
  • Conditions:
    • Stars when i == 1, i == n, j == 1, or j == m.

Code Concept:

for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
        if (i == 1 || i == n || j == 1 || j == m) {
            System.out.print("*");
        } else {
            System.out.print(" "); // for hollow spaces
        }
    }
    System.out.println();
}

Problem 3: Half Pyramid

  • Structure: Rows = Columns based on row number.
  • Logic:
    • Rows determine how many stars to print.

Code Example:

int n = 4;
for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= i; j++) {
        System.out.print("*");
    }
    System.out.println();
}

Problem 4: Inverted Half Pyramid

  • Structure: First row with n stars, last with 1 star.
  • Logic: Decrease number of stars each row.

Code Example:

for (int i = n; i >= 1; i--) {
    for (int j = 1; j <= i; j++) {
        System.out.print("*");
    }
    System.out.println();
}

Problem 5: Inverted Half Pyramid with Spaces

  • Logic: Increment spaces as stars are decremented.
  • Pattern:
    • First row has n-1 spaces + 1 star, etc.

Code Example:

for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n - i; j++) {
        System.out.print(" ");
    }
    for (int j = 1; j <= i; j++) {
        System.out.print("*");
    }
    System.out.println();
}

Problem 6: Floyd's Triangle

  • Pattern: Number 1 to n in a triangular format.
  • Logic:
    • Keep track of the next number to print.

Code Example:

int number = 1;
for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= i; j++) {
        System.out.print(number + " ");
        number++;
    }
    System.out.println();
}

Problem 7: 0-1 Triangle

  • Pattern: Alternate values of 1 and 0 based on cell sum.
  • Logic:
    • Even sums = 1, Odd sums = 0.

Code Example:

for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= i; j++) {
        if ((i + j) % 2 == 0) {
            System.out.print("1 ");
        } else {
            System.out.print("0 ");
        }
    }
    System.out.println();
}

Conclusion

  • Patterns can be solved using nested loops.
  • Importance of analyzing row-column relationships.
  • Next video will continue with more advanced problems.
  • Keep learning and practicing!