Need help on the pyramid number!
Write a program that displays a pyramid pattern. Use a nested for the loop that prints the following output:
http://i1009.photobucket.com/albums/...n/project1.jpg
Hint
Here is the pseudo code solution:
for the row from 0 to 7 {
// Pad leading blanks in a row using a loop like this
for the column from 1 to 7 - row
System.out.print(" ");
// Print left half of the row for numbers 1, 2, 4, up to 2^row using a look like this:
for the column from 0 to row
System.out.print(" " + (int)Math.pow(2, column));
// Print the right half of the row for numbers 2^(row-1),2^(row-2), ..., 1
using a loop like this
for (int column = row - 1; column >= 0; column--)
System.out.print(" " + number = (int)Math.pow(2, column));
// Start a new line
System.out.print('\n');
}
MY PROGRAM SO FAR. What am i missing??? please help!
Code :
import javax.swing.JOptionPane;
public class PyramidProject1_TanNguyen {
public static void main(String[] args) {
for (int row = 0; row <= 7; row++) {
for (int column = 1; column <= 7 - row; column++)
System.out.print("___");
for (int num = row; num >= row; num--)
System.out.print(" " + (int)Math.pow(2, row-7));
System.out.print(" " + (int)Math.pow(2, row-6));
System.out.print(" " + (int)Math.pow(2, row-5));
System.out.print(" " + (int)Math.pow(2, row-4));
System.out.print(" " + (int)Math.pow(2, row-3));
System.out.print(" " + (int)Math.pow(2, row-2));
System.out.print(" " + (int)Math.pow(2, row-1));
System.out.print(" " + (int)Math.pow(2, row));
for (int column = row-1; column >= 0; column--)
System.out.print(" " + (int)Math.pow(2, column));
System.out.println();
}
}
}
Output
http://i1009.photobucket.com/albums/...est_output.jpg