Foundations Working with digits and divisors

28. Count the Odd Digits

Easy

Given an integer n, return how many of its digits are odd. The odd digits are 1, 3, 5, 7 and 9.

Every digit counts on its own, so a 5 that appears three times adds 3 to the answer. A negative number has the same digits as its absolute value, because the minus sign is not a digit. The number 0 has a single digit, 0, which is even, so the answer for 0 is 0.

Example 1

Input: n = 7305921

Output: 5

Explanation: The digits are 7, 3, 0, 5, 9, 2 and 1. Of these, 7, 3, 5, 9 and 1 are odd.

Example 2

Input: n = -3355

Output: 4

Explanation: The minus sign is not a digit. All four digits, 3, 3, 5 and 5, are odd, and each one counts.

Example 3

Input: n = 0

Output: 0

Explanation: 0 has one digit, and 0 is even.

Check your understanding

Work out the answer for this input before you write any code.

n = -13052

Constraints

  • -9 × 1015 ≤ n ≤ 9 × 1015

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.