Close
Close

on runing below code nothing is displayed.If I remove a=a+1 then it is printing why so? ignore the header & other declaration

   mahi6683

for (a=4;a<=100;a++)
    {
        if ((a%2!=0)&&(a%3!=0))
           {
               printf("%d\n",a);
           }

     a=a+1;

    }

 


Answers

  •   

    This is because on writing a=a+1, the if condition will be checked only when the value of a is a multiple of 2 (4, 6, 8, etc). In that case, the condition will become false and so nothing will be printed.

    Here is a more detailed explanation.

    First iteration of for loop (a=4).

    • if condition is checked when a=4. Therefore the condition becomes false and nothing gets printed.
    • a=a+1 makes a=5.

    Second iteration of for loop (a=6 because a++ increments value of a by 1)

    • if condition is checked when a=6. Again the condition becomes false and nothing gets printed.
    • a=a+1 makes a=7.

    and this goes on.

    If you remove a=a+1, the condition of if will be checked when the value of a is a natural number greater than 4 (4, 5, 6, 7, etc).



Ask Yours
Post Yours
Write your answer