Results 1 to 2 of 2
  1. #1
    ftrengnr is offline Member
    Join Date
    Sep 2010
    Location
    Oklahoma
    Posts
    14
    Rep Power
    0

    Question How to format the print statement

    i have to print the powers of 2 from 0-7 in a pyramid. i have the right amount of rows and the right numbers in the rows but i do not know how to format it to make a pyramid shape

    here is what i have so far:

    public class numberPyrimid {

    //main method.
    public static void main(String[] args) {

    //create variable
    int pwr;
    int nmbr;
    String output;

    //create loops for rows and length of rows

    //length of rows
    for(int i = 1; i<=15;i+=2){
    output = "";
    //figure power for row
    pwr = i/2;

    //amount of rows
    for(int x = 0; x<=pwr;x++){

    //make math. return int
    nmbr = (int) Math.pow(2, x);

    //print for ramp up
    //System.out.printf(nmbr+" ");

    output = output+Integer.toString(nmbr)+" ";

    }
    //loop for ramp down
    for(int x = (pwr-1); x >= 0 ; x--){

    //make math. return int
    nmbr= (int)Math.pow(2, x);

    //print for ramp down
    //System.out.print(nmbr+" ");

    output = output+Integer.toString(nmbr)+" ";


    }

    //print out
    System.out.printf(" "+output);
    //print next line
    System.out.println();

    }




    }

    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,611
    Rep Power
    25

    Default

    how to format it to make a pyramid shape
    Print leading spaces, with fewer on each line.

Similar Threads

  1. problems with print statement
    By soccer_kid_6 in forum New To Java
    Replies: 8
    Last Post: 03-22-2010, 03:24 PM
  2. Replies: 18
    Last Post: 12-02-2009, 01:01 PM
  3. how to convert one format to another format
    By mahipal_reddy621 in forum New To Java
    Replies: 1
    Last Post: 12-02-2008, 11:21 AM
  4. Print the text file and print preview them
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-23-2008, 12:04 AM
  5. Statement or Prepared Statement ?
    By paty in forum JDBC
    Replies: 3
    Last Post: 08-01-2007, 05:45 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •