For these exercises, provide only the code needed to answer the question(s) 2) T
ID: 3578855 • Letter: F
Question
For these exercises, provide only the code needed to answer the question(s)
2) The following code snippet is supposed to start from value a to value b, inclusive and print out any value that is divisible 5. For example, if a is 5 and b is 21, the program should print 5, 10, 15, and 20. The code below doesn’t meet this requirement. Find the error(s) and describe how you would fix it. Do not just show the code fix. Describe in your own words how this program should be fixed for (int c = a; c < b; c++) if (c % 5 == 1) System.out.println(c);
Explanation / Answer
Yes there is a bug in it:
Here is correct code:
for (int c = a; c < b; c++)
if (c % 5 == 0)
System.out.println(c);
So what i have changed.
if (c % 5 == 0)
previously c %5 == 1 which means that all the number which are (divisible by 5) +1 are printed
i.e 6, 11 , 16 are printed
if (c % 5 == 0) what this prints is number which are prefectly divisible by 5 and leaves reminder 0 are printed 5,10,15,20