Java programming Part 3 (Single Loops) A loop that contains no inner (or nested)
ID: 3879750 • Letter: J
Question
Java programming
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
/*
Unusual: There is one thing that is unusual. When the day is 64, sum became -1 becuase the number exceeded the limit for the size of type long. If we take int, then it will be unusual a bit early only.
*/
class Main {
public static void countGrains()
{
// declaring variables
long sum = 0;
long eachDay = 1;
//
for(int day=1; day<= 64; day++)
{
// for sum
sum = sum + eachDay;
// printing output
System.out.printf("Day %d and you got %d grain(s) of rice for a total of %d grain(s) ",day, eachDay,sum);
// for each day, to double the rice
eachDay = eachDay + eachDay;
}
}
public static void main(String[] args) {
// calling method
countGrains();
}
}
/*SAMPLE OUTPUT
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 4 and you got 8 grain(s) of rice for a total of 15 grain(s)
Day 5 and you got 16 grain(s) of rice for a total of 31 grain(s)
Day 6 and you got 32 grain(s) of rice for a total of 63 grain(s)
Day 7 and you got 64 grain(s) of rice for a total of 127 grain(s)
Day 8 and you got 128 grain(s) of rice for a total of 255 grain(s)
Day 9 and you got 256 grain(s) of rice for a total of 511 grain(s)
Day 10 and you got 512 grain(s) of rice for a total of 1023 grain(s)
Day 11 and you got 1024 grain(s) of rice for a total of 2047 grain(s)
Day 12 and you got 2048 grain(s) of rice for a total of 4095 grain(s)
Day 13 and you got 4096 grain(s) of rice for a total of 8191 grain(s)
Day 14 and you got 8192 grain(s) of rice for a total of 16383 grain(s)
Day 15 and you got 16384 grain(s) of rice for a total of 32767 grain(s)
Day 16 and you got 32768 grain(s) of rice for a total of 65535 grain(s)
Day 17 and you got 65536 grain(s) of rice for a total of 131071 grain(s)
Day 18 and you got 131072 grain(s) of rice for a total of 262143 grain(s)
Day 19 and you got 262144 grain(s) of rice for a total of 524287 grain(s)
Day 20 and you got 524288 grain(s) of rice for a total of 1048575 grain(s)
Day 21 and you got 1048576 grain(s) of rice for a total of 2097151 grain(s)
Day 22 and you got 2097152 grain(s) of rice for a total of 4194303 grain(s)
Day 23 and you got 4194304 grain(s) of rice for a total of 8388607 grain(s)
Day 24 and you got 8388608 grain(s) of rice for a total of 16777215 grain(s)
Day 25 and you got 16777216 grain(s) of rice for a total of 33554431 grain(s)
Day 26 and you got 33554432 grain(s) of rice for a total of 67108863 grain(s)
Day 27 and you got 67108864 grain(s) of rice for a total of 134217727 grain(s)
Day 28 and you got 134217728 grain(s) of rice for a total of 268435455 grain(s)
Day 29 and you got 268435456 grain(s) of rice for a total of 536870911 grain(s)
Day 30 and you got 536870912 grain(s) of rice for a total of 1073741823 grain(s)
Day 31 and you got 1073741824 grain(s) of rice for a total of 2147483647 grain(s)
Day 32 and you got 2147483648 grain(s) of rice for a total of 4294967295 grain(s)
Day 33 and you got 4294967296 grain(s) of rice for a total of 8589934591 grain(s)
Day 34 and you got 8589934592 grain(s) of rice for a total of 17179869183 grain(s)
Day 35 and you got 17179869184 grain(s) of rice for a total of 34359738367 grain(s)
Day 36 and you got 34359738368 grain(s) of rice for a total of 68719476735 grain(s)
Day 37 and you got 68719476736 grain(s) of rice for a total of 137438953471 grain(s)
Day 38 and you got 137438953472 grain(s) of rice for a total of 274877906943 grain(s)
Day 39 and you got 274877906944 grain(s) of rice for a total of 549755813887 grain(s)
Day 40 and you got 549755813888 grain(s) of rice for a total of 1099511627775 grain(s)
Day 41 and you got 1099511627776 grain(s) of rice for a total of 2199023255551 grain(s)
Day 42 and you got 2199023255552 grain(s) of rice for a total of 4398046511103 grain(s)
Day 43 and you got 4398046511104 grain(s) of rice for a total of 8796093022207 grain(s)
Day 44 and you got 8796093022208 grain(s) of rice for a total of 17592186044415 grain(s)
Day 45 and you got 17592186044416 grain(s) of rice for a total of 35184372088831 grain(s)
Day 46 and you got 35184372088832 grain(s) of rice for a total of 70368744177663 grain(s)
Day 47 and you got 70368744177664 grain(s) of rice for a total of 140737488355327 grain(s)
Day 48 and you got 140737488355328 grain(s) of rice for a total of 281474976710655 grain(s)
Day 49 and you got 281474976710656 grain(s) of rice for a total of 562949953421311 grain(s)
Day 50 and you got 562949953421312 grain(s) of rice for a total of 1125899906842623 grain(s)
Day 51 and you got 1125899906842624 grain(s) of rice for a total of 2251799813685247 grain(s)
Day 52 and you got 2251799813685248 grain(s) of rice for a total of 4503599627370495 grain(s)
Day 53 and you got 4503599627370496 grain(s) of rice for a total of 9007199254740991 grain(s)
Day 54 and you got 9007199254740992 grain(s) of rice for a total of 18014398509481983 grain(s)
Day 55 and you got 18014398509481984 grain(s) of rice for a total of 36028797018963967 grain(s)
Day 56 and you got 36028797018963968 grain(s) of rice for a total of 72057594037927935 grain(s)
Day 57 and you got 72057594037927936 grain(s) of rice for a total of 144115188075855871 grain(s)
Day 58 and you got 144115188075855872 grain(s) of rice for a total of 288230376151711743 grain(s)
Day 59 and you got 288230376151711744 grain(s) of rice for a total of 576460752303423487 grain(s)
Day 60 and you got 576460752303423488 grain(s) of rice for a total of 1152921504606846975 grain(s)
Day 61 and you got 1152921504606846976 grain(s) of rice for a total of 2305843009213693951 grain(s)
Day 62 and you got 2305843009213693952 grain(s) of rice for a total of 4611686018427387903 grain(s)
Day 63 and you got 4611686018427387904 grain(s) of rice for a total of 9223372036854775807 grain(s)
Day 64 and you got -9223372036854775808 grain(s) of rice for a total of -1 grain(s)
*/
/*
Unusual: There is one thing that is unusual. When the day is 64, sum became -1 becuase the number exceeded the limit for the size of type long. If we take int, then it will be unusual a bit early only.
*/
class Main {
public static void countGrains()
{
// declaring variables
long sum = 0;
long eachDay = 1;
//
for(int day=1; day<= 64; day++)
{
// for sum
sum = sum + eachDay;
// printing output
System.out.printf("Day %d and you got %d grain(s) of rice for a total of %d grain(s) ",day, eachDay,sum);
// for each day, to double the rice
eachDay = eachDay + eachDay;
}
}
public static void main(String[] args) {
// calling method
countGrains();
}
}
/*SAMPLE OUTPUT
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 4 and you got 8 grain(s) of rice for a total of 15 grain(s)
Day 5 and you got 16 grain(s) of rice for a total of 31 grain(s)
Day 6 and you got 32 grain(s) of rice for a total of 63 grain(s)
Day 7 and you got 64 grain(s) of rice for a total of 127 grain(s)
Day 8 and you got 128 grain(s) of rice for a total of 255 grain(s)
Day 9 and you got 256 grain(s) of rice for a total of 511 grain(s)
Day 10 and you got 512 grain(s) of rice for a total of 1023 grain(s)
Day 11 and you got 1024 grain(s) of rice for a total of 2047 grain(s)
Day 12 and you got 2048 grain(s) of rice for a total of 4095 grain(s)
Day 13 and you got 4096 grain(s) of rice for a total of 8191 grain(s)
Day 14 and you got 8192 grain(s) of rice for a total of 16383 grain(s)
Day 15 and you got 16384 grain(s) of rice for a total of 32767 grain(s)
Day 16 and you got 32768 grain(s) of rice for a total of 65535 grain(s)
Day 17 and you got 65536 grain(s) of rice for a total of 131071 grain(s)
Day 18 and you got 131072 grain(s) of rice for a total of 262143 grain(s)
Day 19 and you got 262144 grain(s) of rice for a total of 524287 grain(s)
Day 20 and you got 524288 grain(s) of rice for a total of 1048575 grain(s)
Day 21 and you got 1048576 grain(s) of rice for a total of 2097151 grain(s)
Day 22 and you got 2097152 grain(s) of rice for a total of 4194303 grain(s)
Day 23 and you got 4194304 grain(s) of rice for a total of 8388607 grain(s)
Day 24 and you got 8388608 grain(s) of rice for a total of 16777215 grain(s)
Day 25 and you got 16777216 grain(s) of rice for a total of 33554431 grain(s)
Day 26 and you got 33554432 grain(s) of rice for a total of 67108863 grain(s)
Day 27 and you got 67108864 grain(s) of rice for a total of 134217727 grain(s)
Day 28 and you got 134217728 grain(s) of rice for a total of 268435455 grain(s)
Day 29 and you got 268435456 grain(s) of rice for a total of 536870911 grain(s)
Day 30 and you got 536870912 grain(s) of rice for a total of 1073741823 grain(s)
Day 31 and you got 1073741824 grain(s) of rice for a total of 2147483647 grain(s)
Day 32 and you got 2147483648 grain(s) of rice for a total of 4294967295 grain(s)
Day 33 and you got 4294967296 grain(s) of rice for a total of 8589934591 grain(s)
Day 34 and you got 8589934592 grain(s) of rice for a total of 17179869183 grain(s)
Day 35 and you got 17179869184 grain(s) of rice for a total of 34359738367 grain(s)
Day 36 and you got 34359738368 grain(s) of rice for a total of 68719476735 grain(s)
Day 37 and you got 68719476736 grain(s) of rice for a total of 137438953471 grain(s)
Day 38 and you got 137438953472 grain(s) of rice for a total of 274877906943 grain(s)
Day 39 and you got 274877906944 grain(s) of rice for a total of 549755813887 grain(s)
Day 40 and you got 549755813888 grain(s) of rice for a total of 1099511627775 grain(s)
Day 41 and you got 1099511627776 grain(s) of rice for a total of 2199023255551 grain(s)
Day 42 and you got 2199023255552 grain(s) of rice for a total of 4398046511103 grain(s)
Day 43 and you got 4398046511104 grain(s) of rice for a total of 8796093022207 grain(s)
Day 44 and you got 8796093022208 grain(s) of rice for a total of 17592186044415 grain(s)
Day 45 and you got 17592186044416 grain(s) of rice for a total of 35184372088831 grain(s)
Day 46 and you got 35184372088832 grain(s) of rice for a total of 70368744177663 grain(s)
Day 47 and you got 70368744177664 grain(s) of rice for a total of 140737488355327 grain(s)
Day 48 and you got 140737488355328 grain(s) of rice for a total of 281474976710655 grain(s)
Day 49 and you got 281474976710656 grain(s) of rice for a total of 562949953421311 grain(s)
Day 50 and you got 562949953421312 grain(s) of rice for a total of 1125899906842623 grain(s)
Day 51 and you got 1125899906842624 grain(s) of rice for a total of 2251799813685247 grain(s)
Day 52 and you got 2251799813685248 grain(s) of rice for a total of 4503599627370495 grain(s)
Day 53 and you got 4503599627370496 grain(s) of rice for a total of 9007199254740991 grain(s)
Day 54 and you got 9007199254740992 grain(s) of rice for a total of 18014398509481983 grain(s)
Day 55 and you got 18014398509481984 grain(s) of rice for a total of 36028797018963967 grain(s)
Day 56 and you got 36028797018963968 grain(s) of rice for a total of 72057594037927935 grain(s)
Day 57 and you got 72057594037927936 grain(s) of rice for a total of 144115188075855871 grain(s)
Day 58 and you got 144115188075855872 grain(s) of rice for a total of 288230376151711743 grain(s)
Day 59 and you got 288230376151711744 grain(s) of rice for a total of 576460752303423487 grain(s)
Day 60 and you got 576460752303423488 grain(s) of rice for a total of 1152921504606846975 grain(s)
Day 61 and you got 1152921504606846976 grain(s) of rice for a total of 2305843009213693951 grain(s)
Day 62 and you got 2305843009213693952 grain(s) of rice for a total of 4611686018427387903 grain(s)
Day 63 and you got 4611686018427387904 grain(s) of rice for a total of 9223372036854775807 grain(s)
Day 64 and you got -9223372036854775808 grain(s) of rice for a total of -1 grain(s)
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.