Java Character Pattern Programs

Introduction

Character pattern programs are another popular topic for learning and practicing loops and nested loops in Java. These patterns involve printing alphabetic characters in different shapes such as triangles, pyramids, and diamonds. They help in mastering loops and string manipulation in Java. In this post, we'll cover various character patterns along with detailed explanations and Java code examples.


1. Right-Angled Triangle Character Pattern

This pattern forms a right-angled triangle where characters start from A and increment with each row.

Example:

A
AB
ABC
ABCD
ABCDE

Java Program:

public class RightAngledCharacterTriangle {
    public static void main(String[] args) {
        int rows = 5; // Number of rows

        // Outer loop for rows
        for (int i = 1; i <= rows; i++) {
            char ch = 'A'; // Starting character

            // Inner loop to print characters from 'A' to current row number
            for (int j = 1; j <= i; j++) {
                System.out.print(ch);
                ch++; // Move to the next character
            }
            System.out.println(); // Move to the next line
        }
    }
}

Explanation:

  • The outer loop controls the number of rows.
  • The inner loop prints characters starting from A, incrementing by one for each column.

2. Inverted Right-Angled Triangle Character Pattern

This pattern prints an inverted right-angled triangle of characters, starting from A and decreasing with each row.

Example:

ABCDE
ABCD
ABC
AB
A

Java Program:

public class InvertedRightAngledCharacterTriangle {
    public static void main(String[] args) {
        int rows = 5; // Number of rows

        // Outer loop for rows
        for (int i = rows; i >= 1; i--) {
            char ch = 'A'; // Starting character

            // Inner loop to print characters from 'A' to current row number
            for (int j = 1; j <= i; j++) {
                System.out.print(ch);
                ch++; // Move to the next character
            }
            System.out.println(); // Move to the next line
        }
    }
}

Explanation:

  • The outer loop controls the rows, starting from rows and decreasing.
  • The inner loop prints characters up to the current row number.

3. Pyramid Character Pattern

This pattern prints a pyramid shape with characters starting from A at the top, expanding as the rows increase.

Example:

    A
   ABA
  ABCBA
 ABCDCBA
ABCDEDCBA

Java Program:

public class PyramidCharacterPattern {
    public static void main(String[] args) {
        int rows = 5; // Number of rows

        // Outer loop for rows
        for (int i = 1; i <= rows; i++) {
            // Print leading spaces for centering
            for (int j = i; j < rows; j++) {
                System.out.print(" ");
            }

            // Print increasing characters
            char ch = 'A';
            for (int j = 1; j <= i; j++) {
                System.out.print(ch);
                ch++;
            }

            // Print decreasing characters
            ch -= 2;
            for (int j = 1; j < i; j++) {
                System.out.print(ch);
                ch--;
            }

            System.out.println(); // Move to the next line
        }
    }
}

Explanation:

  • The first inner loop prints spaces to center the characters.
  • The second and third loops print the increasing and decreasing characters to form the pyramid shape.

4. Inverted Pyramid Character Pattern

An inverted pyramid where characters start from A at the widest part and shrink as the rows decrease.

Example:

ABCDEDCBA
 ABCDCBA
  ABCBA
   ABA
    A

Java Program:

public class InvertedPyramidCharacterPattern {
    public static void main(String[] args) {
        int rows = 5; // Number of rows

        // Outer loop for rows
        for (int i = rows; i >= 1; i--) {
            // Print leading spaces for centering
            for (int j = i; j < rows; j++) {
                System.out.print(" ");
            }

            // Print increasing characters
            char ch = 'A';
            for (int j = 1; j <= i; j++) {
                System.out.print(ch);
                ch++;
            }

            // Print decreasing characters
            ch -= 2;
            for (int j = 1; j < i; j++) {
                System.out.print(ch);
                ch--;
            }

            System.out.println(); // Move to the next line
        }
    }
}

Explanation:

  • The first inner loop prints spaces to center the characters.
  • The second and third loops print the increasing and decreasing characters for the inverted pyramid shape.

5. Character Diamond Pattern

A diamond-shaped pattern with characters, expanding and then contracting symmetrically.

Example:

    A
   ABA
  ABCBA
 ABCDCBA
ABCDEDCBA
 ABCDCBA
  ABCBA
   ABA
    A

Java Program:

public class CharacterDiamondPattern {
    public static void main(String[] args) {
        int rows = 5; // Number of rows

        // Print the upper half of the diamond
        for (int i = 1; i <= rows; i++) {
            for (int j = i; j < rows; j++) {
                System.out.print(" ");
            }

            char ch = 'A';
            for (int j = 1; j <= i; j++) {
                System.out.print(ch);
                ch++;
            }

            ch -= 2;
            for (int j = 1; j < i; j++) {
                System.out.print(ch);
                ch--;
            }
            System.out.println();
        }

        // Print the lower half of the diamond
        for (int i = rows - 1; i >= 1; i--) {
            for (int j = i; j < rows; j++) {
                System.out.print(" ");
            }

            char ch = 'A';
            for (int j = 1; j <= i; j++) {
                System.out.print(ch);
                ch++;
            }

            ch -= 2;
            for (int j = 1; j < i; j++) {
                System.out.print(ch);
                ch--;
            }
            System.out.println();
        }
    }
}

Explanation:

  • The first half prints an expanding character pattern (pyramid).
  • The second half prints a contracting pattern (inverted pyramid), forming the diamond shape.

6. Right-Aligned Triangle Character Pattern

A right-aligned triangle where characters increment by row.

Example:

    A
   AB
  ABC
 ABCD
ABCDE

Java Program:

public class RightAlignedCharacterTriangle {
    public static void main(String[] args) {
        int rows = 5; // Number of rows

        // Outer loop for rows
        for (int i = 1; i <= rows; i++) {
            // Print leading spaces for alignment
            for (int j = i; j < rows; j++) {
                System.out.print(" ");
            }

            char ch = 'A';
            // Print characters for each row
            for (int j = 1; j <= i; j++) {
                System.out.print(ch);
                ch++;
            }

            System.out.println(); // Move to the next line
        }
    }
}

Explanation:

  • The first loop prints spaces to right-align the characters.
  • The second loop prints the characters starting from A and incrementing as rows increase.

7. Left-Aligned Triangle Character Pattern

A left-aligned triangle where characters increment with each row.

Example:

A
AB
ABC
ABCD
ABCDE

Java Program:

public class LeftAlignedCharacterTriangle {
    public static void main(String[] args) {
        int rows = 5; // Number of rows

        // Outer loop for rows
        for (int i = 1; i <= rows; i++) {
            char ch = 'A';

            // Inner loop to print characters
            for (int j = 1; j <= i; j++) {
                System.out.print(ch);
                ch++;
            }
            System.out.println(); // Move to the next line
        }
    }
}

Explanation:

  • The outer loop controls the rows, and the inner loop prints characters starting from A and incrementing with each row.

8. Repeated Character Triangle Pattern

A triangle where each row contains repeated characters, starting from A.

Example:

A
BB
CCC
DDDD
EEEEE

Java Program:

public class RepeatedCharacterTriangle {
    public static void main(String[] args) {
        int rows = 5; // Number of rows

        // Outer loop for rows
        for (int i = 1; i <= rows; i++) {
            char ch = (char) ('A' + i - 1); // Current character for the row



            // Inner loop to print repeated characters
            for (int j = 1; j <= i; j++) {
                System.out.print(ch);
            }
            System.out.println(); // Move to the next line
        }
    }
}

Explanation:

  • The outer loop controls the number of rows.
  • The inner loop prints the same character multiple times in each row, with the character being the same as the row number (starting from A).

9. Hollow Square Character Pattern

A hollow square with characters where only the border of the square contains characters, and the inside is hollow.

Example:

AAAAA
A   A
A   A
A   A
AAAAA

Java Program:

public class HollowSquareCharacterPattern {
    public static void main(String[] args) {
        int size = 5; // Size of the square

        // Outer loop for rows
        for (int i = 1; i <= size; i++) {
            // Inner loop for columns
            for (int j = 1; j <= size; j++) {
                if (i == 1 || i == size || j == 1 || j == size) {
                    System.out.print("A");
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println(); // Move to the next line
        }
    }
}

Explanation:

  • The outer loop controls the rows, and the inner loop prints characters only on the border of the square.
  • The inside of the square is hollow, represented by spaces.

10. Character Sandglass Pattern

A sandglass-shaped character pattern where characters decrement and increment symmetrically.

Example:

ABCDEDCBA
 ABCDCBA
  ABCBA
   ABA
    A
   ABA
  ABCBA
 ABCDCBA
ABCDEDCBA

Java Program:

public class CharacterSandglassPattern {
    public static void main(String[] args) {
        int rows = 5; // Number of rows

        // Upper part of the sandglass
        for (int i = rows; i >= 1; i--) {
            // Print leading spaces
            for (int j = i; j < rows; j++) {
                System.out.print(" ");
            }

            char ch = 'A';
            // Print increasing characters
            for (int j = 1; j <= i; j++) {
                System.out.print(ch);
                ch++;
            }

            // Print decreasing characters
            ch -= 2;
            for (int j = 1; j < i; j++) {
                System.out.print(ch);
                ch--;
            }
            System.out.println(); // Move to the next line
        }

        // Lower part of the sandglass
        for (int i = 2; i <= rows; i++) {
            // Print leading spaces
            for (int j = i; j < rows; j++) {
                System.out.print(" ");
            }

            char ch = 'A';
            // Print increasing characters
            for (int j = 1; j <= i; j++) {
                System.out.print(ch);
                ch++;
            }

            // Print decreasing characters
            ch -= 2;
            for (int j = 1; j < i; j++) {
                System.out.print(ch);
                ch--;
            }
            System.out.println(); // Move to the next line
        }
    }
}

Explanation:

  • The first part prints the upper half of the sandglass pattern (inverted pyramid).
  • The second part prints the lower half of the sandglass (normal pyramid), creating a symmetric shape.

Conclusion

These 10 Java character pattern programs cover a variety of shapes such as triangles, pyramids, diamonds, and squares. By practicing these character patterns, you can enhance your understanding of loops, conditional statements, and character manipulation in Java. These patterns are commonly asked in coding interviews and are a great way to improve your problem-solving skills.

Comments