Computer Science EngineeringHardISRO SC (CS)

Consider the following C fragment. int c=0; for(int i=1;i<=18;i++) for(int j=1;j<=i;j++) if((i+j)%3==0) c++; What is c at the end?

  1. A. 57
  2. B. 51
  3. C. 114
  4. D. 29

Correct answer: A. 57

Simulating the loops gives 57. The distractors come from the usual slips: off-by-one at a boundary (< vs <=), dropping a loop level, or using a cruder asymptotic estimate instead of the exact count.

More Algorithms, Data Structures & Programming (ISRO CS) questions

Exams that ask this topic