- #1
carl123
- 56
- 0
There is a story of the inventor of chess. The king wanted to reward him with riches, but he only asked for the following. One grain of wheat for the first square, two on the second, four on the third, doubling the amount for each square until 64 squares are accounted for.
Questions
1) Design a program for calculating the total number of grains of wheat. You will want to use iteration/looping. Do not forget to keep track of which square you are on and the number of grains of wheat that you currently have.
2) Write the program to determine the number of squares needed to get at least 1000, 1,000,000 and 1,000,000,000 grains of wheat.My code so far, works perfectly for number 1, Not sure how to go about number 2.
Questions
1) Design a program for calculating the total number of grains of wheat. You will want to use iteration/looping. Do not forget to keep track of which square you are on and the number of grains of wheat that you currently have.
2) Write the program to determine the number of squares needed to get at least 1000, 1,000,000 and 1,000,000,000 grains of wheat.My code so far, works perfectly for number 1, Not sure how to go about number 2.
Code:
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int calc = 1;
int sqNum = 1;
unsigned long long total = 1;
cout << "Square " << sqNum << " has " << total << " grain of wheat" << endl;
for (int i = 1; i<64; ++i){
sqNum++;
calc = (calc * 2);
total = total + calc;
cout << "Square " << sqNum << " has " << total << " grains of wheat " << endl;
}
return 0;
}