in Programming in C edited by
1,848 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

26 Comments

20 times
0
0

@ ma'am

can you explain?

0
0

@Lakshman Patel RJIT

u answer it, I will correct it, if u do any mistake :)

is it ok?:)

0
0
edited by
when $i=0$ we print $\text{"GreatIndia"}$
0
0

1. i=0.  //initialization

2.i % 3 == 0  // Condition checking, and  condition is true

3. Print "Great" 

4. i%5 == 0 // Condition checking, and  condition is true

5. Print "India"


Now,

1. i=1.  //initialization

2.i % 3 == 0  //Condition checking , and condition is false

4. i%5 == 0 // Condition checking, and  condition is False

Nothing Printed.


Similar for i=2

Same as above

Nothing printed here too


1. i=3.  //initialization

2.i % 3 == 0  // Condition checking, and  condition is true

3. Print "Great" 

4. i%5 == 0 // Condition checking, and  condition is False

5. Not Print "India"


i=4

no. condition will be true // same as i=2


1. i=5.  //initialization

2.i % 3 == 0  // Condition checking, and  condition is False

3. Not Print "Great" 

4. i%5 == 0 // Condition checking, and  condition is true

5. Print "India"


Now, 

Similarly i=6 will Print "Great"

But here not getting India


Again for i=9 will print "Great"


i=10 will get "India"


Similarly if we calculate in i=15 , we get total 3 "Great India"

So, from this we can say , in i=90, we get 18 "Great India"

Now, from 90 to 100 we get 2 more Great India

So, total 20

3
3
you have to check line by line

Donot combine the lines
1
1
Yes ma'am
But i got $21$ see my above comment
1
1
yes, 21

So, ans D)

right?
2
2
I check three times and I got $21$,so I think $(D)$ is the correct answer.
0
0
edited by

@ sir

please check this

If we write like this

int main() 
{
  for(int i=0; i<=100;i++) 
  {
      if (i % 3 == 0 && i%5 == 0)
      { 
        printf("GreatIndia");  
      }
  }

  return 0;
}

Divisible by $3$ and divisible by $5$ means divisible by $\text{LCM(3,5)=15}$

${\color{Green}{i=0,15,30,45,60,75,90} }$

We print ${\color{Magenta} {\text{GreatIndia}}}$  $7$ times.

If we write like this

int main() 
{
  for(int i=0; i<=100;i++) 
  {
      if (i % 3 == 0 || i%5 == 0)
      { 
        printf("GreatIndia");  
      }
  }

  return 0;
}

Divisible by $3$ (or) divisible by $5$ 

For $i=0$  we print ${\color{DarkOrange}{"GreatIndia"} }$

and from ${\color{Purple} {i=1,2,3,...,100}}$

we can apply principle of mutual  inclusion and exclusion

${\color{Magenta} {|A\cup B|=|A|+|B|-|A\cap B|}}$

Here $A=3,B=5$

$|3\cup 5| = |3| + |5| - |3\cap 5|$--------->$(1)$

Now,$|3| =\left \lfloor \frac{100}{3} \right \rfloor=33$

       $|5|=\left \lfloor \frac{100}{5} \right \rfloor=20$

       $|3\cap 5|=\left \lfloor \frac{100}{LCM(3,5)} \right \rfloor=\left \lfloor \frac{100}{15} \right \rfloor=6$

Put all values in equation $(1)$ and we can

$|3\cup 5|=33+20-6=53-6=47$

So total we can print $1+47=48$

We print ${\color{Red} {\text{GreatIndia}}}$  $48$ times.

But given question in a different way

So,we print like this

$i=0,0$---->(1)

$i=3,5$--->(2)

$i=9,10$----->(3)

$i=15,15$----->(4)

$i=18,20$---->(5)

$i=24,25$---->(6)

$i=30,30$---->(7)

$i=33,35$---->(8)

$i=39,40$---->(9)

$i=45,45$---->(10)

$i=48,50$---->(11)

$i=51,55$---->(12)

$i=60,60$---->(13)

$i=63,65$---->(14)

$i=69,70$---->(15)

$i=75,75$---->(16)

$i=78,80$---->(17)

$i=84,85$---->(18)

$i=90,90$---->(19)

$i=93,95$---->(20)

$i=99,100$---->(21)

Why i neglect i = 12 case?

When $i=12$ it is divisible by $3$

So it print ${\color{Red}{\text{Great}} }$

But after that $i$ become $15$ then it is divisible by $3$ as well as $5$

So,it will print ${\color{DarkOrange}{\text{GreatIndia}} }$

So, finally answer is $21$

3
3
Yes option d is the correct answer
0
0
moved by
Possible values of i are { 0,15,30,45,60,75,90} , i.e there are 7 values where "GreatIndia" is printed. So D is the answer.
0
0

@

please see my above comment. Answer must be $21$

When you don't explain the answer please write in the comment section.

0
0
Ok i realised the flaw now.
0
0
answer should be 7 multiple of 15 are found
0
0

@

the answer should be $21$, please solve again.

0
0
Okay it means I have to take every possible combination of multiple of 3 and 5 here
0
0
For I = 0 nothing will be will be printed
0
0
$i=0$

"GreatIndia" will be printed.
0
0
For i=0 great india will be printed.
0
0
how 0 mod 3 wouldn't be equal to zero
0
0
0 mod 3 means remainder when we divide 0 by 3 so it is 0
0
0
Okay got confused it's zero is divided by three not the other way
0
0
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