Write a Chess class that will compute the solution to the wheat and chessboard p
ID: 3863760 • Letter: W
Question
Write a Chess class that will compute the solution to the wheat and chessboard problem: If a chessboard were to have wheat placed upon each square such that one grain were placed on the first square, two on the second, four on the third, and so on (doubling the number of grains on each subsequent square), how many grains of wheat would be on the chessboard at the finish? the program will compute: the total grains of wheat to cover the whole board the number of grains on the last square (Square 64).Explanation / Answer
import java.util.Scanner; public class Chess { public static void main(String args[]){ double noOfSquares, noOfWheatsOnLastSquare, totalWheats; //Either you can hard code no of sqaures as 64 like noOfSquares=64 and remove line number 7,8,9 and inport as well or you can take input from user Scanner sc=new Scanner(System.in); System.out.print("Enter number of squares: "); noOfSquares = sc.nextDouble(); //This is the formula for calculating total grains of wheat on chess board. totalWheats = (Math.pow(2, noOfSquares) -1); //If you look at the pattern of the grains of wheat arranged then: // at position 1 - 1 (2^0) // at position 2 - 1 (2^1) // at position 3 - 1 (2^2) // at position 4 - 1 (2^3) and so on //Hence at last position, sin our case 64 - 1 (2^63) noOfWheatsOnLastSquare = (Math.pow(2, noOfSquares-1)); System.out.println("The total grains of wheat to cover whole board are: " + totalWheats); System.out.println("The number of grains on last square are: " + noOfWheatsOnLastSquare); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.