简体   繁体   中英

How to count the non-null digits of a number and the digits divisible by 3

if (c % 3 == 0)
{
    d=d+1;
}
else
{
    cout << "The number has no digits divisible with 3" << endl;
}

But the thing is, c was used before in a while structure because I had to use it to calculate the sum of the number's digits and other things. If I try to write this outside of the while, I believe c will get the value only of the first digit of the number because of the loop. I tried giving the value o c to another variable but it's still the same.

Maybe you should try putting your logic in another while loop, similar to the one you used before.

You can also try calculating the number of digits divisible by 3 in the same loop that you calculate the sum in

while(...)
{
    ...
    sum = sum + c;
    if(c%3 == 0)
    {
        d = d+1;
    }
    ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM