You are given an integer n. You need to return the number of odd digits present in the number.
The number will have no leading zeroes, except when the number is 0 itself.
Input: n = 5
Output: 1
Explanation: 5 is an odd digit.
Input: n = 25
Output: 1
Explanation: The only odd digit in 25 is 5.
Input: n = 15
Given a number, all the digits in the number can be extracted one by one from right to left which can be checked for even and odd.
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
/* Function to count number
of odd digits in N */
int countOddDigit(int n) {
/* Counter to store the
number of odd digits */
int oddDigits = 0;
// Iterate till there are digits left
while (n > 0) {
// Extract last digit
int lastDigit = n % 10;
// Check if digit is odd
if (lastDigit % 2 != 0) {
// Increment counter
oddDigits = oddDigits + 1;
}
n = n / 10;
}
return oddDigits;
}
};
int main() {
int n = 6678;
/* Creating an instance of
Solution class */
Solution sol;
// Function call to get count of odd digits in n
int ans = sol.countOddDigit(n);
cout << "The count of odd digits in the given number is: " << ans;
return 0;
}
class Solution {
/* Function to count number
of odd digits in N */
public int countOddDigit(int n) {
/* Counter to store the
number of odd digits */
int oddDigits = 0;
// Iterate till there are digits left
while (n > 0) {
// Extract last digit
int lastDigit = n % 10;
// Check if digit is odd
if (lastDigit % 2 != 0) {
// Increment counter
oddDigits = oddDigits + 1;
}
n = n / 10;
}
return oddDigits;
}
public static void main(String[] args) {
int n = 6678;
/* Creating an instance of
Solution class */
Solution sol = new Solution();
// Function call to get count of odd digits in n
int ans = sol.countOddDigit(n);
System.out.println("The count of odd digits in the given number is: " + ans);
}
}
class Solution:
# Function to count number
# of odd digits in N
def countOddDigit(self, n):
# Counter to store the
# number of odd digits
oddDigits = 0
# Iterate till there are digits left
while n > 0:
# Extract last digit
lastDigit = n % 10
# Check if digit is odd
if lastDigit % 2 != 0:
# Increment counter
oddDigits = oddDigits + 1
n = n // 10
return oddDigits
# Input number
n = 6678
# Creating an instance of
# Solution class
sol = Solution()
# Function call to get count of odd digits in n
ans = sol.countOddDigit(n)
print("The count of odd digits in the given number is:", ans)
class Solution {
/* Function to count number
of odd digits in N */
countOddDigit(n) {
/* Counter to store the
number of odd digits */
let oddDigits = 0;
// Iterate till there are digits left
while (n > 0) {
// Extract last digit
let lastDigit = n % 10;
// Check if digit is odd
if (lastDigit % 2 !== 0) {
// Increment counter
oddDigits = oddDigits + 1;
}
n = Math.floor(n / 10);
}
return oddDigits;
}
}
// Input number
let n = 6678;
/* Creating an instance of
Solution class */
let sol = new Solution();
// Function call to get count of odd digits in n
let ans = sol.countOddDigit(n);
console.log("The count of odd digits in the given number is: " + ans);
Time Complexity: O(log10(N)) – In every iteration we are dividing N by 10 (equivalent to the number of digits in N).
Space Complexity: O(1) – Using only couple of variables i.e., constant space.