Viktor - 1 year ago 63

C++ Question

I have a variable which contains this numbers

`int n = 6396339;`

I need to determine how many digits 3 is in variable.

So far i trued like this:

`int n = 6396339, counter = 0;`

while (n > 0)

{

if ((n % 10) % 3 == 0) {

counter++;

}

n /= 10;

}

cout << counter << endl;

But this algorithm is not working correctly. Could you please help me to solve problem.

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

```
(n % 10) % 3 == 0
```

is true for any digit that is divisible by three, i.e. `0`

, `3`

, `6`

and `9`

. Just check whether the digit is equal to `3`

:

```
(n % 10) == 3
```

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**