edited by
1,780 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
edited by

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 votes
1 votes

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.

Related questions

3 votes
3 votes
4 answers
1
manikgupta123 asked Apr 29, 2019
1,766 views
What is the time complexity to delete an arbitrary node from binary heap?O(n)O(log n)O(1)O(n log n)
3 votes
3 votes
4 answers
2
manikgupta123 asked Apr 28, 2019
1,658 views
What is the time complexity for insertion in binary tree in worst case?O(1)O(log n)O(n)O(n log n)
1 votes
1 votes
4 answers
3
manikgupta123 asked Apr 29, 2019
1,304 views
Which of the following gives O(1) complexity if we want to check whether an edge exists between two given nodes in a graph?Adjacency ListAdjacency MatrixIncidence MatrixN...
0 votes
0 votes
1 answer
4
manikgupta123 asked Apr 29, 2019
970 views
Which part in 8086 microprocessor is responsible for fetching instructions into the queue?BIUEUStackRegisters