Hi Everyone, Can someone help me write the code for the following number pyramid:
print("
1
2 0 2
3 0 0 0 3
4 0 0 0 0 0 4
... ");
There is 5 spaces in between each number. So far I have this:
print("import java.util.Scanner;
class Pyramid {
public static void main(String[] args) { Scanner input = new Scanner (System.in); System.out.print("Please enter a number: "); int x =input.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--)
System.out.print((k >= 10) ?+ k : " " + k);
System.out.println();
}
} } ");
and this prints out:
print("
1
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
");
import java.util.Scanner; class Pyramid {
public static void main(String[] args) { Scanner input = new Scanner (System.in); System.out.print("Please enter a number: "); int x =input.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--)
System.out.print((k >= 10) ?+ k : " " + k);
System.out.println();
}
} }