Programming in C - Decision Making and Looping

46. Consider the following program fragment
d = 0;
for(i = 1; i < 31; ++i)
 for(j = 1; j < 31; ++j)
  for(k = 1; k < 31; ++k)
   if(((i + j + k) % 3) == 0)
    d = d + 1;
printf("%d", d );
The output will be

  • Option : A
  • Explanation :
    a + b + c 3 will be 0 if a + b + c is a multiple of 3.
    This will happen in one of the following ways.
    All three - a, b, and c are multiples of 3. This can only happen if a, b, and c take one of the 10 values.
    - 3 , 6, 9 , ..... , 30, independent of one another. So, there are 10 x 10 x 10 = 1000 ways this can happen.
    Another possibility is that a, b, and c all leave a remainder 1 so that a + b + c is evenly divisible by 3.
    Considering all the different possibilities and adding. we get 9000. That will be the integer that gets printed.
Cancel reply

Your email address will not be published. Required fields are marked *


Cancel reply

Your email address will not be published. Required fields are marked *