recategorized by
720 views
0 votes
0 votes

Question:-

Consider the following program fragment

a = 0
for (x = 1; x < 31; ++x)
for (y = 1; y < 31; ++y)
  for (z = 1; z < 31; ++z)
    if (((x + y + z)%3) = =0)
    a = a + 1;
printf("%d”, a);

the output of the above given program is

Option (A) : 3000

Option (B) : 1000

Option(C) : 27000

Option(D) : None of these

recategorized by

1 Answer

Best answer
2 votes
2 votes
The ans is D : none of this

It is given to us that :

a = 0
for (x = 1; x < 31; ++x)
for (y = 1; y < 31; ++y)
  for (z = 1; z < 31; ++z)
    if (((x + y + z)%3) = =0)   <-This statement will execute 30*30*30 times.
    a = a + 1;
It will execute 27000 times.

out of this for one complete iteration of the inner most loop if we see we will get 10 values for which the inner most condition will be satisfied and the value of a will be incremented. for 1st iteration:

1,1,1 ;1,1,4;1,1,7;1,1,10;1,1,13;1,1,16;1,1,19;1,1,22;1,1,25;1,1,28; i.e(31-1)/3=10

10 iterations because only 10 times we get a num which is one more than the multiple of 3 in the first iteration as see the value of x and y is fixed i.e 1+1 =2 so to satisfy the condition we need 1,4,7,so on so it is possible only for 10 values in the given range(arithmetic progression with common diff 3).

Next time also value of x and y will be fixed =1+2=3. Then the reqd no would be 3,6,9... so on for all iterations.

Total no of times the value of a will be incremented is 30*30*10=9000

So ans is 9000 and option D
edited by

Related questions

0 votes
0 votes
2 answers
1
kallu singh asked Aug 12, 2017
511 views
Consider the following C programint f1(int n) { if(n == 0 || n == 1) return n; else return (2*f1(n-1) + 3*f1(n-2)); }Consider the program given in above question, f1(8) a...
0 votes
0 votes
1 answer
2
kallu singh asked Dec 15, 2017
249 views
0 votes
0 votes
1 answer
3
kallu singh asked Jan 17, 2019
467 views
0 votes
0 votes
0 answers
4