Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Programming language is C++. Show code and an example of the output on Putty whe

ID: 3783757 • Letter: P

Question

Programming language is C++. Show code and an example of the output on Putty when done.

his reward. The inventor asked for one grain of rice for the first square, 2 for the second, 4 for the third, and so on, doubling for each of the 64 squares. That may sound modest, but there wasn't that much rice in the empire! Write a program to calculate how many squares are required to give the inventor at least 1000 grains of rice, at least 1,000,000 grains, and at least 1,000,000,000 grains. You'll need a loop, of course, and probably an int to keep track of which square you are at, an int to keep the number ofgrains on the current square, and an int to keep track of the grains on all previous squares. We suggest that you write out the value of all your variables for each iteration of the loop so that you can see what's going on.

Explanation / Answer

the below program runs a loop to for aa the squares and sq is the loop variable and it is initialized to 1 for first square

gr indicates no of grains for each square

tot indicates total no of grains till then for all squares

and count for counting occurences of 1000,1000000 and 1000000 in the loop and to prevent the loop from printing the message everytime the condition satisfies

Source code:

#include<stdio.h>
int main ()
{

int sq=1;
   long int gr=1;
   long int tot=1;
   long int count=0;

   for( sq=2;sq<=64;sq++)
   {
   gr=gr*2;
   tot=tot+gr;

   if(tot>=1000 && count<1)
   {
       count++;
       printf("%d squares are required to get atleast 1000 grains ",sq);

   }
   if(tot>=1000000 && count <2)
   {
       count++;

printf("%d squares are required to get atleast 1000000 grains ",sq);
      
   }
   if(tot>=1000000000 && count <3)
   {
       count++;
       printf("%d squares are required to get atleast 1000000000 grains ",sq);
break;
}
}

}

Output:

10squares are required to get atleast 1000 grains
20squares are required to get atleast 1000000 grains
30squares are required to get atleast 1000000000 grains