You are given an integer n. You need to return the number of digits in the number.
The number will have no leading zeroes, except when the number is 0 itself.
Input: n = 4
Output: 1
Explanation: There is only 1 digit in 4.
Input: n = 14
Output: 2
Explanation: There are 2 digits in 14.
Input: n = 234
Given a number, all the digits in the number can be counted by counting one by one every digit which can be done by extracting the last digit successively from the right until there are no more digits left to extract.
What if the given number is zero?
Return 1, because the number of digits in zero is 1.
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
// Function to count all digits in n
int countDigit(int n) {
// Edge case
if(n == 0) return 1;
// Set counter to 0
int cnt = 0;
// Iterate until n is greater than zero
while(n > 0) {
// Increment count of digits
cnt = cnt + 1;
n = n / 10;
}
return cnt;
}
};
int main()
{
int n = 6678;
/* Creating and instance of
Solution class */
Solution sol;
// Function call to get count of digits in n
int ans = sol.countDigit(n);
cout << "The count of digits in the given number is: " << ans;
return 0;
}
class Solution {
// Function to count all digits in n
public int countDigit(int n) {
// Edge case
if (n == 0) return 1;
// Set counter to 0
int cnt = 0;
// Iterate until n is greater than zero
while (n > 0) {
// Increment count of digits
cnt = cnt + 1;
n = n / 10;
}
return cnt;
}
}
class Main {
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 digits in n
int ans = sol.countDigit(n);
System.out.println("The count of digits in the given number is: " + ans);
}
}
class Solution:
# Function to count all digits in n
def countDigit(self, n):
# Edge case
if n == 0:
return 1
# Set counter to 0
cnt = 0
# Iterate until n is greater than zero
while n > 0:
# Increment count of digits
cnt = cnt + 1
n = n // 10
return cnt
# Input number
n = 6678
# Creating an instance of Solution class
sol = Solution()
# Function call to get count of digits in n
ans = sol.countDigit(n)
print(f"The count of digits in the given number is: {ans}")
class Solution {
// Function to count all digits in n
countDigit(n) {
// Edge case
if (n === 0) return 1;
// Set counter to 0
let cnt = 0;
// Iterate until n is greater than zero
while (n > 0) {
// Increment count of digits
cnt = cnt + 1;
n = Math.floor(n / 10);
}
return cnt;
}
}
// Input number
let n = 6678;
/* Creating an instance of
Solution class */
let sol = new Solution();
// Function call to get count of digits in n
let ans = sol.countDigit(n);
console.log("The count of digits in the given number is: " + ans);
Time Complexity: O(log10(N)) – In every iteration we are dividing N by 10.
Space Complexity: O(1) – Using a couple of variables i.e., constant space.
Given a number, the number of digits can be found directly from the logarithm (base 10) of the number.
What if the given number is zero?
Return 1, because the number of digits in zero is 1.
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
/* Function to count the
number of odd digits in N */
int countDigit(int n) {
// Edge case
if(n == 0) return 1;
int count = log10(n) + 1;
return count;
}
};
int main()
{
int n = 6678;
/* Creating an instance of
Solution class */
Solution sol;
// Function call to get count of digits in n
int ans = sol.countDigit(n);
cout << "The count of digits in the given number is: " << ans;
return 0;
}
class Solution {
/* Function to count the
number of odd digits in N */
public int countDigit(int n) {
// Edge case
if (n == 0) return 1;
int count = (int)(Math.log10(n) + 1);
return count;
}
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 digits in n
int ans = sol.countDigit(n);
System.out.println("The count of digits in the given number is: " + ans);
}
}
import math
class Solution:
# Function to count the
# number of odd digits in N
def countDigit(self, n):
# Edge case
if n == 0:
return 1
count = int(math.log10(n) + 1)
return count
# Input number
n = 6678
# Creating an instance of Solution class
sol = Solution()
# Function call to get count of digits in n
ans = sol.countDigit(n)
print("The count of digits in the given number is:", ans)
class Solution {
/* Function to count the
number of odd digits in N */
countDigit(n) {
// Edge case
if (n === 0) return 1;
let count = Math.floor(Math.log10(n) + 1);
return count;
}
}
// Input number
let n = 6678;
// Creating an instance of Solution class
let sol = new Solution();
// Function call to get count of digits in n
let ans = sol.countDigit(n);
console.log("The count of digits in the given number is: " + ans);
Time Complexity: O(1) – Constant time taking statements were used.
Space Complexity: O(1) – Because only a couple of variables were used.