Print this pattern in java
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Answers
public class Pattern {
public static void main(String[] args) {
int rows = 5;
for(int i = 1; i <= rows; ++i) {
for(int j = 1; j <= i; ++j) {
System.out.print("* ");
}
System.out.println();
}
}
}
Answer:
public class pattern16 {
public static void main(String[] args) {
int n = 5, val = 1;
for (int row = 0; row < n; row++) {
for (int spaces = 1; spaces <= n - row; spaces++) {
System.out.print(" ");
}
for (int col = 0; col <= row; col++) {
if (col == 0 || row == n)
System.out.print("1");
else
System.out.print(" " + (val = val * (row - col + 1) / col));
}
System.out.println("");
}
}
}
Explanation: