Problem Statement: Given an integer N, write a program to count the number of digits in N.
Examples
Example 1: Input: N = 12345 Output: 5 Explanation: N has 5 digits Example 2: Input: N = 8394 Output: 4 Explanation: N has 4 digits
Disclaimer: Don’t jump directly to the solution, try it out yourself first.
Solution 1:
Algorithm / Intuition
Approach:
- Store the integer in a variable X and initialize a counter variable to count the number of digits.
- We know that in programming languages when we divide X by Y it will result in an integer (given both the variables are integers). For example, 133/10 will result in 13 similarly 1/10 will result in 0.
- Using a for loop and above observation keep on dividing X by 10 and increment the count in every iteration when X equals 0 terminate the loop and the count will have the number of digits in N.
Code
#include<bits/stdc++.h>
using namespace std;
int count_digits( int n )
{
int x = n; int count =0;
while( x !=0 )
{
x = x / 10;
count++;
}
return count;
}
int main()
{
int n = 12345;
cout<< "Number of digits in "<<n<<" is "<<count_digits(n);
return 0;
}
Output: Number of digits in 12345 is 5
public class tUf {
static int count_digits(int n)
{
int x = n; int count =0;
while( x!=0 )
{
x = x / 10;
count++;
}
return count;
}
public static void main(String args[])
{
int n = 12345;
System.out.println("Number of digits in "+n+" is "+count_digits(n));
}
}
Output: Number of digits in 12345 is 5
def count_digits(n):
count=0
x=n
while( x != 0 ):
x//=10
count+=1
return count
n = 12345
print("Number of digits in ",n," is ",count_digits(n))
Output: Number of digits in 12345 is 5
function countDigits(n) {
let x = n;
let count = 0;
while (x !== 0) {
x = Math.floor(x / 10);
count++;
}
return count;
}
let n = 12345;
console.log("Number of digits in " + n + " is " + countDigits(n));
Output: Number of digits in 12345 is 5
Complexity Analysis
Time Complexity: O (n) where n is the number of digits in the given integer
Space Complexity: O(1)
Solution 2:
Algorithm / Intuition
Solution 2 :
- Convert the integer into a string.
- Find the length of the string
Code
#include<bits/stdc++.h>
using namespace std;
int count_digits( int n )
{
string x = to_string(n);
return x.length();
}
int main()
{
int n = 12345;
cout<< "Number of digits in "<<n<<" is "<<count_digits(n);
return 0;
}
Number of digits in 12345 is 5
import java.util.*;
public class tUf {
static int count_digits(int n) {
String n2 = Integer.toString(n);
return n2.length();
}
public static void main(String args[]) {
int n = 12345;
System.out.println("Number of digits in " + n + " is " + count_digits(n));
}
}
Number of digits in 12345 is 5
def count_digits(n):
x = str(n)
return len(x)
n = 12345
print("Number of digits in ", n, " is ", count_digits(n))
Number of digits in 12345 is 5
function countDigits(n) {
let x = n.toString();
return x.length;
}
let n = 12345;
console.log("Number of digits in " + n + " is " + countDigits(n));
Number of digits in 12345 is 5
Complexity Analysis
Time Complexity: O (1)
Space Complexity: O(1)
Solution 3:
Algorithm / Intuition
Use logarithm base 10 to count the number of digits. As
The number of digits in an integer = the upper bound of log10(n).
Example :
n = 12345 log10(12345) = 4.091 Integral part of 4.091 is 4 and 4 + 1 = 5 which is also the number of digits in 12345
Code
#include<bits/stdc++.h>
using namespace std;
int count_digits( int n )
{
int digits = floor(log10(n) + 1);
return digits;
}
int main()
{
int n = 12345;
cout<< "Number of digits in "<<n<<" is "<<count_digits(n);
return 0;
}
Output: Number of digits in 12345 is 5
import java.util.*;
public class tUf {
static int count_digits(int n)
{
int digits = (int) Math.floor(Math.log10(n) + 1);
return digits;
}
public static void main(String args[])
{
int n = 12345;
System.out.println("Number of digits in "+n+" is "+count_digits(n));
}
}
Output: Number of digits in 12345 is 5
import math
def count_digits(n):
digits = math.floor(math.log10(n) + 1)
return digits
n = 12345
print("Number of digits in ", n, " is ", count_digits(n))
Output: Number of digits in 12345 is 5
function countDigits(n) {
let digits = Math.floor(Math.log10(n) + 1);
return digits;
}
let n = 12345;
console.log("Number of digits in " + n + " is " + countDigits(n));
Output: Number of digits in 12345 is 5
Complexity Analysis
Time Complexity: O (1)
Space Complexity: O(1)
Video Explanation
Special thanks to Harsh Verma for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article