The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

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

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

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with Premium.
Start your 7-day free trial.

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trialbtw, the DecimalFormat bit limits it to 10 decimal places, and the while loop pads the strings out to a max of 15 chars :-)

Tim

import java.text.* ;

import java.math.BigInteger;

class Numbers

{

public static void main( String[] args )

{

BigInteger bi = new BigInteger("2");

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

System.out.println("------

for(int i = 1;i < 64;i++) {

String current = bi.pow(i).toString();

while(current.length() < 19)

current = " " + current;

System.out.println(i + "\t" + current);

}

} //end of main

} //end of class

StringBuffer out = new StringBuffer();

out.setLength(10 - s.length());

out.append(s);

I'd also suggest not using tabs to align your table and instead add the number of spaces you require.

{

long answer = 0;

for (byte n=1; n<64 && (answer=(long) Math.pow(2, n))<10000000000L; n++)

{

rightAlignValue(n, 2);

rightAlignValue(answer, 15);

System.out.println();

}

}

private static void rightAlignValue(long value, int len)

{

String s = Long.toString(value);

StringBuffer out = new StringBuffer();

out.setLength(len - s.length());

out.append(s);

System.out.print(out);

}

Java

From novice to tech pro — start learning today.

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with Premium.
Start your 7-day free trial.

-----------

import java.text.* ;

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

DecimalFormat df = new DecimalFormat( "0.##########E00" ) ;

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

String sAnswer = df.format( answer ) ;

while( sAnswer.length() < 15 )

sAnswer = " " + sAnswer ;

System.out.println( n + "\t" + sAnswer ) ; //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