Viktor - 4 months ago 24

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.

Answer

```
(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
```