Post History
C (gcc), 37 bytes This takes an integer as input. It uses the fact that the last digit is odd if and only if the number is odd, and that integer division by 10 removes the last digit. Testing if ...
Answer
#3: Post edited
- # [C (gcc)], 37 bytes
- This takes an integer as input. It uses the fact that the last digit is odd if and only if the number is odd, and that integer division by 10 removes the last digit.
- <!-- language-all: lang-c -->
- r;f(i){for(;i;i/=10)r+=i&1;return r;}
- [Try it online!][TIO-m20ofyvg]
- [C (gcc)]: https://gcc.gnu.org/
- [TIO-m20ofyvg]: https://tio.run/##Dcg7DsIwDADQ3aewikCOyi8wWaachAUlDVgqKXLTqerZA2984fAKoVaTROqWNBqJip46f3bWdrrzYn2ZLaPJWjeawzDHHm9TiToe33cAzQU/T83kYAHEr/0jUbONj9zsMdGF2TNfmZ0TWOsP "C (gcc) – Try It Online"
- # [C (gcc)], 37 bytes
- This takes an integer as input. It uses the fact that the last digit is odd if and only if the number is odd, and that integer division by 10 removes the last digit.
- Testing if the number is odd is by doing bitwise and with 1, which conveniently gives 1 for odd and 0 for even numbers, so the number of odd digits is obtained by just adding those up.
- <!-- language-all: lang-c -->
- r;f(i){for(;i;i/=10)r+=i&1;return r;}
- [Try it online!][TIO-m20ofyvg]
- [C (gcc)]: https://gcc.gnu.org/
- [TIO-m20ofyvg]: https://tio.run/##Dcg7DsIwDADQ3aewikCOyi8wWaachAUlDVgqKXLTqerZA2984fAKoVaTROqWNBqJip46f3bWdrrzYn2ZLaPJWjeawzDHHm9TiToe33cAzQU/T83kYAHEr/0jUbONj9zsMdGF2TNfmZ0TWOsP "C (gcc) – Try It Online"
#2: Post edited
# [C (gcc)], 38 bytes- This takes an integer as input. It uses the fact that the last digit is odd if and only if the number is odd, and that integer division by 10 removes the last digit.
- <!-- language-all: lang-c -->
r;f(i){while(i)r+=i&1,i/=10;return r;}[Try it online!][TIO-m20o8axo]- [C (gcc)]: https://gcc.gnu.org/
[TIO-m20o8axo]: https://tio.run/##S9ZNT07@/7/IOk0jU7O6PCMzJxXIKNK2zVQz1MnUtzU0sC5KLSktylMosq79r5yZl5xTmpKqYFNckpKZr5dhx8WVmVeikJuYmaehyVXNpaBQUAQUSNNQUk2JyVPSUUjTsIQBTU1rrtr/AA "C (gcc) – Try It Online"
- # [C (gcc)], 37 bytes
- This takes an integer as input. It uses the fact that the last digit is odd if and only if the number is odd, and that integer division by 10 removes the last digit.
- <!-- language-all: lang-c -->
- r;f(i){for(;i;i/=10)r+=i&1;return r;}
- [Try it online!][TIO-m20ofyvg]
- [C (gcc)]: https://gcc.gnu.org/
- [TIO-m20ofyvg]: https://tio.run/##Dcg7DsIwDADQ3aewikCOyi8wWaachAUlDVgqKXLTqerZA2984fAKoVaTROqWNBqJip46f3bWdrrzYn2ZLaPJWjeawzDHHm9TiToe33cAzQU/T83kYAHEr/0jUbONj9zsMdGF2TNfmZ0TWOsP "C (gcc) – Try It Online"
#1: Initial revision
# [C (gcc)], 38 bytes This takes an integer as input. It uses the fact that the last digit is odd if and only if the number is odd, and that integer division by 10 removes the last digit. <!-- language-all: lang-c --> r;f(i){while(i)r+=i&1,i/=10;return r;} [Try it online!][TIO-m20o8axo] [C (gcc)]: https://gcc.gnu.org/ [TIO-m20o8axo]: https://tio.run/##S9ZNT07@/7/IOk0jU7O6PCMzJxXIKNK2zVQz1MnUtzU0sC5KLSktylMosq79r5yZl5xTmpKqYFNckpKZr5dhx8WVmVeikJuYmaehyVXNpaBQUAQUSNNQUk2JyVPSUUjTsIQBTU1rrtr/AA "C (gcc) – Try It Online"