Viktor - 9 months ago 42
C++ Question

# How to determine, how many number of digits 3 is in variable

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;


(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