Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Post History

60%
+1 −0
Challenges How many odd digits?

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 ...

posted 1mo ago by celtschk‭  ·  edited 1mo ago by celtschk‭

Answer
#3: Post edited by user avatar celtschk‭ · 2024-10-08T16:55:47Z (about 1 month ago)
Added a bit more explanation
  • # [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 by user avatar celtschk‭ · 2024-10-08T16:51:52Z (about 1 month ago)
  • # [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 by user avatar celtschk‭ · 2024-10-08T16:47:58Z (about 1 month ago)
# [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"