Pattern problem
One Pattern
Print in the output the exact pattern provided below 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
public class PatternPrinter {
public static void main(String[] args) {
int rows = 5; // Number of lines
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("1 ");
}
System.out.println(); // Move to next line
}
}
}
Number Pattern
Print in the output the exact pattern provided below 1 2 3 4 5 1 2 3 4 1 2 3 1 2 1
public class ReversePattern { public static void main(String[] args) { int rows = 5; // Number of rows for (int i = rows; i >= 1; i--) { for (int j = 1; j <= i; j++) { System.out.print("1 "); } System.out.println(); // Move to next line } } }
V Pattern
Print in the output the exact pattern provided below
1 1
12 21
123 321
1234 4321
1234554321
public class VNumberPattern {
public static void main(String[] args) {
int n = 5; // number of rows
for (int i = 1; i <= n; i++) {
// Print increasing numbers from 1 to i
for (int j = 1; j <= i; j++) {
System.out.print(j);
}
// Print spaces: 2*(n - i) spaces
for (int j = 1; j <= 2 * (n - i); j++) {
System.out.print(" ");
}
// Print decreasing numbers from i to 1
for (int j = i; j >= 1; j--) {
System.out.print(j);
}
System.out.println(); // move to next line
}
}
}
Comments
Post a Comment