0% found this document useful (0 votes)
3 views

Java Pattern Programs– Questions and Answers for Beginners

The document provides Java code examples for creating various star and number patterns, including a right-angled triangle, square, pyramid, number triangle, and diamond. Each pattern is accompanied by its output and a brief code snippet demonstrating how to generate it. These examples serve as practice for understanding loops and pattern generation in Java.

Uploaded by

nagavelli09
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)
3 views

Java Pattern Programs– Questions and Answers for Beginners

The document provides Java code examples for creating various star and number patterns, including a right-angled triangle, square, pyramid, number triangle, and diamond. Each pattern is accompanied by its output and a brief code snippet demonstrating how to generate it. These examples serve as practice for understanding loops and pattern generation in Java.

Uploaded by

nagavelli09
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/ 3

Java Pattern Programs - Questions and Answers

1. Print a Right-Angled Triangle Pattern

Output:
*
**
***
****
*****
Code:
public class RightTriangle {
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. Print a Square Star Pattern

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

3. Print a Pyramid Pattern

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

4. Print a Number Triangle Pattern

Output:
1
12
123
1234
12345
Code:
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. Print a Diamond Pattern

Output:
*
***
*****
***
*
Code:
public class DiamondPattern {
public static void main(String[] args) {
int n = 3;
for(int i = 1; i <= n; i++) {
for(int j = i; j < n; j++) System.out.print(" ");
for(int k = 1; k <= (2*i-1); k++) System.out.print("*");
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("*");
System.out.println();
}
}
}

You might also like