# Programming tips

There once was a wise servant who saved the life of a princess. The king promised to pay whatever the servant could dream up. Knowing that the king loved chess, the servant told the king he would like to have grains of wheat. One grain on the first square of a chess board. Two grains on the next. Four on the third, and so on.

There are 64 squares on a chessboard.

Write a program that shows how many grains were on each square and the total number of grains.

In other words, I want to type

java Grains

and see

square 1:  1 grain
square 2:  2 grains
square 3:  4 grains
square 4:  8 grains

etc.

public class Chess {
public static void main(String[] args) {
try{
int enteredNumber = Integer.parseInt(args[0]);
if( enteredNumber < 1 || enteredNumber > 64){
System.out.print("invalid numbers");
System.exit(0);
}
int numberOfSquares = (int)Math.pow(2, (enteredNumber - 1));
System.out.println(numberOfSquares);
}catch(NumberFormatException e){
e.printStackTrace();
}
}
}
LVL 2
###### Who is Participating?
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.

Commented:
Run a for loop from 0 to 63 that does the calculation for you
for(int i=0;i<64;i++)
{
numberOfSquares = (int) Math.pow(2,i);
sysout(numberOfSquares);
}

Right now the program is only running for a single number you need to change the dependency on the args[0].

Experts Exchange Solution brought to you by