Solved

# Length

Posted on 2003-11-19

hey,

i'ver develop a programme using a while and for loop to diplay in a table the results of 2^n where 1<=n<64.

However i don't know how to modify the loop to limit the resuts to 10 digits. Also i wish to right align the numbers displayed in the n column.

If anyone knows how to do this, please let me know. If ye want to jazz up the presentation too fire away! thanks!

Here is my code:

/*Computes 2^n where 1<=n<64 and displays in a table*/

class Numbers

{

public static void main(String[] args)

{

byte n = 1; //declare n, the power, as a byte. initialised at 1 and will not excced 64

double answer; //solution of 2^n

System.out.println("n \t2^n"); //print header of table output

while (n<64) //iterate loop while n is less than 64

{

answer = Math.pow(2,n); // set answer to 2^n

System.out.println(n + "\t" + answer); //print answer

n++; //increment n by 1

} // end of while loop

/*for (n=1; n<64; n++)

{

answer = Math.pow(2,n); // set answer to 2^n

System.out.println(n + "\t" + answer); //print answer

n=n++; //increment n by 1

}//end of for loop*/

}//end of main

}//end of class

Thanks as ever

Dinny