What the hell happens in this damn program!?

using namespace std;

int main ()

{

int x=15;

int y=52;

int t=1;

int i;

for(i=x; i<=y; i++)

{

if((i%2==0) && (i/10%2 !=0))

{

t=t+1;}

}

cout<< t;

cin.ignore();

}

Why is the god damn answer 10!?

2

✅ Answers

? Favorite Answer

  • Because it’s the count (+1) of integers, n, in the range [15 .. 52] for which the following are both true:

    . n is even

    . the remainder of n divided by 10 is not divisible by 2

    16 is even, 16 / 10 = 1, 1 % 2 = 1 (same for 18)

    20 is even, 20 /10 = 2, 2 % 2 = 0 (same for 22, 24, 26, 28, 40, 42, 44, 46, 48)

    30 is even, 30 / 10 = 3, 3 % 2 = 1 (same for 32, 34, 36, 38, 50, 52)

    So there are 9 numbers in this range that satisfy the criteria. You start your counter ‘t’ at 1, so it ends up being 10.

  • if((i%2==0) && (i/10%2 !=0))

    this is only true when the last digit is even and the penultimate digit is odd

    so t is incrmented when:

    16

    18

    30

    32

    34

    36

    38

    50

    52

    so t is incremented 9 times. so t = 1 (initial value) + 9 = 10

  • Leave a Comment