1. create an interactive program that produces the following pyramid.
1
212
32123
4321234
543212345
Java code:
class Project_4_Bonus {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String choice = "Y";
do {
cal_Pyramid();
System.out.print("continue ? >>> (Y/N): ");
choice = sc.next();
System.out.println("\n");
} while (choice.equalsIgnoreCase("Y"));
} //end
public static void cal_Pyramid() {
System.out.print("Enter the highest number of pyramid : ");
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
for (int i = 1; i <= x; i++) {
for (int j = 1; j <= x - i; j++) {
System.out.print(" ");
}
for (int k = i; k >= 1; k--) {
if (k >= 10) {
System.out.print(k);
} else {
System.out.print(" " + k);
}
}
for (int k = 2; k <= i; k++) {
System.out.print((k >= 10) ? k : " " + k);
}
System.out.println();
}
}
}
No comments:
Post a Comment