Please help me to code the following in: JAVA Part 3 (Single Loops) A loop that
ID: 3600710 • Letter: P
Question
Please help me to code the following in: JAVA
Part 3 (Single Loops) A loop that contains no inner (or nested) loop represents a single Repetition Control Structure, and is related to branching in that a boolean expression will govern the behavior of the loop. Loop 0 above is an example of a singular loop, as are the following problems. Once we've practiced with a few individual loops, we'll experiment further with nested loops (loops inside loops, just like the loop 2 above) Starting with one grain of rice, double the number of grains of rice you receive each day. If a king paid you for 64 days, how many grains of rice would you have? Write a short, method with the name countGrains that calculates and prints the number of grains you earn each day and the running sum of all the grains to date Note: Do you observe anything unusual in your output when you run your code? Explain, in comments. Day 1 and you got 1 grain(s) of rice for a total of 1 grain (s) Day 2 and you got 2 grain(s) of rice for a total of 3 grain (s) Day 3 and you got 4 grain(s) of rice for a total of 7 grain (s) Day 30 and you got X grain (s) of rice for a total of Y grain (s) Day 31 and you got X grain (s) of rice for a total of Y grain (s). Day 32 and you got X grain (s) of rice for a total of Y grain (s)Explanation / Answer
Given the code for part 3. Please post part 4 as separate question.
To indent code in eclipse, select code usign Ctrl+A and then Ctrl +i
public class Loops {
private static void countGrains()
{
int grains = 1;
int total = 0;
for(int day = 1; day <= 64; day++)
{
grains *= 2;
total += grains;
System.out.printf("Day %d and you got %d grain(s) of rice for a total of %d grain(s). ", day, grains, total);
}
}
public static void main(String[] args) {
countGrains();
}
}
output
Day 1 and you got 2 grain(s) of rice for a total of 2 grain(s).
Day 2 and you got 4 grain(s) of rice for a total of 6 grain(s).
Day 3 and you got 8 grain(s) of rice for a total of 14 grain(s).
Day 4 and you got 16 grain(s) of rice for a total of 30 grain(s).
Day 5 and you got 32 grain(s) of rice for a total of 62 grain(s).
Day 6 and you got 64 grain(s) of rice for a total of 126 grain(s).
Day 7 and you got 128 grain(s) of rice for a total of 254 grain(s).
Day 8 and you got 256 grain(s) of rice for a total of 510 grain(s).
Day 9 and you got 512 grain(s) of rice for a total of 1022 grain(s).
Day 10 and you got 1024 grain(s) of rice for a total of 2046 grain(s).
Day 11 and you got 2048 grain(s) of rice for a total of 4094 grain(s).
Day 12 and you got 4096 grain(s) of rice for a total of 8190 grain(s).
Day 13 and you got 8192 grain(s) of rice for a total of 16382 grain(s).
Day 14 and you got 16384 grain(s) of rice for a total of 32766 grain(s).
Day 15 and you got 32768 grain(s) of rice for a total of 65534 grain(s).
Day 16 and you got 65536 grain(s) of rice for a total of 131070 grain(s).
Day 17 and you got 131072 grain(s) of rice for a total of 262142 grain(s).
Day 18 and you got 262144 grain(s) of rice for a total of 524286 grain(s).
Day 19 and you got 524288 grain(s) of rice for a total of 1048574 grain(s).
Day 20 and you got 1048576 grain(s) of rice for a total of 2097150 grain(s).
Day 21 and you got 2097152 grain(s) of rice for a total of 4194302 grain(s).
Day 22 and you got 4194304 grain(s) of rice for a total of 8388606 grain(s).
Day 23 and you got 8388608 grain(s) of rice for a total of 16777214 grain(s).
Day 24 and you got 16777216 grain(s) of rice for a total of 33554430 grain(s).
Day 25 and you got 33554432 grain(s) of rice for a total of 67108862 grain(s).
Day 26 and you got 67108864 grain(s) of rice for a total of 134217726 grain(s).
Day 27 and you got 134217728 grain(s) of rice for a total of 268435454 grain(s).
Day 28 and you got 268435456 grain(s) of rice for a total of 536870910 grain(s).
Day 29 and you got 536870912 grain(s) of rice for a total of 1073741822 grain(s).
Day 30 and you got 1073741824 grain(s) of rice for a total of 2147483646 grain(s).
Day 31 and you got -2147483648 grain(s) of rice for a total of -2 grain(s).
Day 32 and you got 0 grain(s) of rice for a total of -2 grain(s).
Day 33 and you got 0 grain(s) of rice for a total of -2 grain(s).
Day 34 and you got 0 grain(s) of rice for a total of -2 grain(s).
Day 35 and you got 0 grain(s) of rice for a total of -2 grain(s).
Day 36 and you got 0 grain(s) of rice for a total of -2 grain(s).
Day 37 and you got 0 grain(s) of rice for a total of -2 grain(s).
Day 38 and you got 0 grain(s) of rice for a total of -2 grain(s).
Day 39 and you got 0 grain(s) of rice for a total of -2 grain(s).
Day 40 and you got 0 grain(s) of rice for a total of -2 grain(s).
Day 41 and you got 0 grain(s) of rice for a total of -2 grain(s).
Day 42 and you got 0 grain(s) of rice for a total of -2 grain(s).
Day 43 and you got 0 grain(s) of rice for a total of -2 grain(s).
Day 44 and you got 0 grain(s) of rice for a total of -2 grain(s).
Day 45 and you got 0 grain(s) of rice for a total of -2 grain(s).
Day 46 and you got 0 grain(s) of rice for a total of -2 grain(s).
Day 47 and you got 0 grain(s) of rice for a total of -2 grain(s).
Day 48 and you got 0 grain(s) of rice for a total of -2 grain(s).
Day 49 and you got 0 grain(s) of rice for a total of -2 grain(s).
Day 50 and you got 0 grain(s) of rice for a total of -2 grain(s).
Day 51 and you got 0 grain(s) of rice for a total of -2 grain(s).
Day 52 and you got 0 grain(s) of rice for a total of -2 grain(s).
Day 53 and you got 0 grain(s) of rice for a total of -2 grain(s).
Day 54 and you got 0 grain(s) of rice for a total of -2 grain(s).
Day 55 and you got 0 grain(s) of rice for a total of -2 grain(s).
Day 56 and you got 0 grain(s) of rice for a total of -2 grain(s).
Day 57 and you got 0 grain(s) of rice for a total of -2 grain(s).
Day 58 and you got 0 grain(s) of rice for a total of -2 grain(s).
Day 59 and you got 0 grain(s) of rice for a total of -2 grain(s).
Day 60 and you got 0 grain(s) of rice for a total of -2 grain(s).
Day 61 and you got 0 grain(s) of rice for a total of -2 grain(s).
Day 62 and you got 0 grain(s) of rice for a total of -2 grain(s).
Day 63 and you got 0 grain(s) of rice for a total of -2 grain(s).
Day 64 and you got 0 grain(s) of rice for a total of -2 grain(s).
Analysis of output
After 30 days... the no. of grains exceeds the maximum value that int data type can store. So the values turn negative because of overflow. We can use a long datatype for grains and total to have bigger values.