mardi 30 avril 2019

Which value of num when initialized would give this nested loop a "true" output?

Had this question on an exam and am pretty sure there's some sort of trick to it.

"Suppose num was initialized to a certain value prior to this loop. Which value of num would give a "true" output?"

int result = 0; 
for (i = 0; i < num; i++)
     if (i % 3 == 1)
     result += i;

     if result == num 
          cout << "True" << endl;
     else
          cout << "False" << endl;

a) 4

b) 5

c) 6

d) 7

Am a newbie programmer. Can anyone interpret what's going on?

Aucun commentaire:

Enregistrer un commentaire