0% found this document useful (0 votes)
14 views4 pages

Java Coding Patterns

The document provides Java coding patterns with examples, including star patterns, number triangles, and Pascal's triangle. Each pattern is demonstrated through a complete Java class with a main method. These coding patterns are commonly asked in interviews and exams.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views4 pages

Java Coding Patterns

The document provides Java coding patterns with examples, including star patterns, number triangles, and Pascal's triangle. Each pattern is demonstrated through a complete Java class with a main method. These coding patterns are commonly asked in interviews and exams.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Java Coding Patterns - Complete PDF

1. Star Patterns

Right-angled triangle

public class StarPattern1 {


public static void main(String[] args) {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("* ");
}
System.out.println();
}
}
}

2. Inverted Star Pattern

public class StarPattern2 {


public static void main(String[] args) {
int n = 5;
for (int i = n; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
System.out.print("* ");
}
System.out.println();
}
}
}

3. Pyramid Pattern

public class PyramidPattern {


public static void main(String[] args) {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = n; j > i; j--) {
System.out.print(" ");
}
for (int k = 1; k <= (2*i - 1); k++) {
System.out.print("*");

1
}
System.out.println();
}
}
}

4. Number Triangle

public class NumberTriangle {


public static void main(String[] args) {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j + " ");
}
System.out.println();
}
}
}

5. Floyd’s Triangle

public class FloydsTriangle {


public static void main(String[] args) {
int n = 5, num = 1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(num + " ");
num++;
}
System.out.println();
}
}
}

6. Diamond Pattern

public class DiamondPattern {


public static void main(String[] args) {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = n; j > i; j--) System.out.print(" ");
for (int k = 1; k <= (2*i - 1); k++) System.out.print("*");
System.out.println();

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

7. Pascal’s Triangle

public class PascalsTriangle {


public static void main(String[] args) {
int n = 5;
for (int i = 0; i < n; i++) {
int number = 1;
for (int j = 0; j <= i; j++) {
System.out.print(number + " ");
number = number * (i - j) / (j + 1);
}
System.out.println();
}
}
}

8. Alphabet Pyramid

public class AlphabetPyramid {


public static void main(String[] args) {
int n = 5;
for (int i = 0; i < n; i++) {
for (int j = n; j > i; j--) System.out.print(" ");
char ch = 'A';
for (int k = 0; k <= i; k++) {
System.out.print(ch + " ");
ch++;
}
System.out.println();
}
}
}

3
9. Number Diamond

public class NumberDiamond {


public static void main(String[] args) {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = n; j > i; j--) System.out.print(" ");
for (int k = 1; k <= (2*i - 1); k++) System.out.print(i);
System.out.println();
}
for (int i = n-1; i >= 1; i--) {
for (int j = n; j > i; j--) System.out.print(" ");
for (int k = 1; k <= (2*i - 1); k++) System.out.print(i);
System.out.println();
}
}
}

These programs cover most common coding patterns asked in interviews and exams.

You might also like