in Programming in C edited by
1,838 views
3 votes
3 votes
for(int i=0; i<=100;i++) {
if (i % 3 == 0)
printf("Great);
if(i%5 == 0)
printf("India");
}

Count the number of times GreatIndia is printed.

  1. 6
  2. 20
  3. 33
  4. none of these
in Programming in C edited by
1.8k views

4 Comments

if this question comes in gate then we will execute the loop step by step for 100 times ?
0
0

@ no, we can observe how condition satisfied and generalize the condition.

0
0
C program
C program 

I think correct answer would be 7 option (D)

Here, In above c program;  I have used variable c which will increment if i is divisible by 3 and 5 both so it will print both “GREAT INDIA” together; and whenever c==2 that means both if loops gets executed post that used ‘d’ variable to count how many times this has happened.

so final answer =7

Please cross check question once;  if outer for loop would have started with i=1 then answer would be 6 because  0 is excluded in that case.

0
0

3 Answers

3 votes
3 votes
It would print exactly 7 times.

We have to find total integers divisible by 3 and 5 between 0 and 100 there are 7 integers divisible by 3 and 5

i.e 0 , 15 , 30  , 45 , 60 , 75 , 90
1 vote
1 vote
Answer : D,

7 times
 for the following values of i
 0, 15, 30, 45, 60, 75, 90
1 vote
1 vote

The answer should be 21. 


From 0 to 100; "GreatIndia" be printed at the following:

a) All multiples of 15 ( 7 times)

b) Every time a multiple of 5 which is not a multiple of 15 is encountered after a multiple of 3, which again should not be a multiple of 15. For eg at i=3 and then at i=5. Then at i=9 and i=10. The last one will be at i=99 and i=100. 

Now from 1 to 15, GreatIndia is printed 3 times. So from 16 to 90 it will be printed another 15 times. From i=91 to i=100, GreatIndia will be printed after i=93 and i=95. The last one will be at i=99 and i=100. And the very first one is at i=0. Adding up, we have GreatIndia printed 21 times.

You can also run the code and check. Put a newline after printf("India") statement for ease in counting.

edited by