user2933251
user2933251

Reputation:

Count how many times a number divides another number

I am trying to count how many 2 divides 100. When I run this code, I got the output of 1 and 2. Can anyone kindly explain how it is 1 2? I know I am using a count to keep tracking of the division but in the while loop how does it work?

int x = 100;
int count = 0;

while (x % 2 == 0)
{
    x = x / 2;
    count++;
    printf("%d", count);
}

Upvotes: 0

Views: 450

Answers (1)

alk
alk

Reputation: 70971

You do not get 12 but a 1 followed by a 2.

Change this

  printf("%d", count);

to be

  printf("%d: x = %d\n", count, x);

and become enlightened. ;)

Upvotes: 2

Related Questions