In the next few articles, we are going to learn some basic math problems that are very crucial to learn Data structures and algorithms.
Let’s start with the COUNT DIGITS IN A NUMBER with solution code in c++, java, and python.
Problem Statement
Given an integer N, write a program to check whether the given number is a prime number or not.
Ex1:
Input N: 11
Output: Prime Number
Ex2:
Input N: 6
Output: Not a Prime Number.
APPROACH1
Read the value of N by taking input and initializing a temporary variable ‘count’ to zero. By using a while loop, divide N by 10 which results in the quotient of N when it is divided by 10. if the value of N is decremented to a single digit number, it results in 0. so, until the base condition satisfies, increment the count variable for each iteration.
ALGORITHM
STEP 1: start
STEP 2: take the input value of N. initializes zero to a temporary variable, let’s take “count”.
STEP 3: iterate the input number through a while loop till the number is greater than zero when it is divided by 10. increment count variable value for each iteration.
STEP 4: Return the count value.
STEP 5: stop.
C++ Code:
#include <bits/stdc++.h>
using namespace std;
int main() {
int N,count=0;
while(N>0){
count+=1;
N=N/10;
}
cout<<”No. Of digits in the number: ”<<count;
}
Python code:
N = int(input())
count = 0
while N>0:
count+=1
N=N//10
print("no. of digits in number: ",count)
JAVA code:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int N = s.nextInt();
int count =0;
while( N!=0 ) {
N = N/10;
count++;
}
System.out.println("no. of digits in number: "+count);
}
}
Input: 1234
Output: No. Of digits in the number: 4
The time complexity of the code is O(n) as here we use a while loop.
The space complexity of the code is O(1).
NOTE:
In c++, an integer datatype takes only the number as input which can maximum have 10 digits, if we want to take a number having more than 10 digits, initialize the variable with a long datatype. Long datatype holds a number maximum of digits 19.
long n=12345678998798L;
APPROACH 2:
By converting the number to a string and returning the length of the string.
ALGORITHM
STEP 1: start
STEP 2: take the input value of N.
STEP 3: convert the number into the string by using pre-defined functions.
STEP 4: Return the length of the string by using pre-defined functions.
STEP 5: stop.
C++ Code:
#include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin>>N;
string x = to_string(N);
cout<<"No. of digits in number: "<<x.length();
}
Python code:
N = int(input())
x = str(N)
print("no. of digits in number: ",len(x))
JAVA code:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int N = s.nextInt();
String n = Integer.toString(N);
int len = n.length();
System.out.println("no. of digits in number: "+len);
}
}
Input: 1234
Output: No. Of digits in the number: 4
The time complexity and the space complexity of the code are O(1).
APPROACH 3:
We can calculate the number of digits in N by using the logarithm of base 10. Logarithm to base 10 returns a float value, for which we can calculate the upper bound, which resembles the number of digits in N.
ALGORITHM
STEP 1: start
STEP 2: take the input value of N.
STEP 3: calculate the logarithm value of the input number by using the logarithm function.
STEP 4: calculate the ceil value of the logarithm value applied to the input number. Return the ceil value.
STEP 5: stop.
C++ Code:
#include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin>>N;
cout<<"no. of digits in number: "<<ceil(log10(N));
}
Python code:
import math
N = int(input())
print("no. of digits in number: ",math.ceil(math.log10(N)))
JAVA code:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int N = s.nextInt();
int digits = (int)Math.ceil(Math.log10(N));
System.out.println("no. of digits in number: "+digits);
}
}
Input: 1234
Output: No. Of digits in the number: 4
The time complexity and the space complexity of the code are O(1).
CONCLUSION
That’s it from this tutorial. Hope you guys found It interesting. We have solved the count of the number of digits in a number in different programming languages. Happy coding!