Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Programming tips

Posted on 2009-07-06
1
Medium Priority
?
316 Views
Last Modified: 2013-11-23
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();
            }
      }
}
0
Comment
Question by:SunScreenCert
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 20

Accepted Solution

by:
a_b earned 2000 total points
ID: 24791766
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].
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question