Java Pattern Printing — Question Paper (100 Problems)
This booklet contains 100 pattern-printing problems (characters, stars, numbers) ranging from moderate to
very difficult. Each question shows the expected figure (example for n = 5 unless specified). Solutions (Java)
are provided at the end as concise, ready-to-run methods labelled pattern1 ... pattern100. Use n as the input
size where applicable.
Q1. Hollow Square
* * * * *
* *
* *
* *
* * * * *
Q2. Pyramid
*
***
*****
*******
*********
Q3. Diamond
*
***
*****
***
*
Q4. Floyd's Triangle
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Q5. Pascal's Triangle
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Note: Only 5 example problems are shown above. The full version includes all 100 problems with their
figures.
Solutions (Java)
public class PatternSolutions {
public static void pattern1(int n) {
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(i==0||i==n-1||j==0||j==n-1) [Link]("* ");
else [Link](" ");
}
[Link]();
}
}
public static void pattern2(int n) {
for(int i=1;i<=n;i++){
for(int s=0;s<n-i;s++) [Link](" ");
for(int j=1;j<=2*i-1;j++) [Link]("*");
[Link]();
}
}
public static void pattern3(int n) {
for(int i=1;i<=n;i++){
for(int s=0;s<n-i;s++) [Link](" ");
for(int j=1;j<=2*i-1;j++) [Link]("*");
[Link]();
}
for(int i=n-1;i>=1;i--){
for(int s=0;s<n-i;s++) [Link](" ");
for(int j=1;j<=2*i-1;j++) [Link]("*");
[Link]();
}
}
public static void pattern4(int n){
int num=1;
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++) [Link]((num++) + " ");
[Link]();
}
}
public static void pattern5(int n){
for(int i=0;i<n;i++){
int val=1;
for(int j=0;j<=i;j++){
[Link](val + " ");
val = val * (i - j) / (j + 1);
}
[Link]();
}
}
}