How to find length of integer in Java
We can find the length of the integer in many ways. The length of an integer is defined as the count of the number of digits for the given integer.
These are the approaches for finding the length of an integer in Java:
- By using the while loop
- By using the String
- By using the Continuous Multiplication
- By using the Logarithm
- By using the Recursion method
1. By using the while loop
The variable n contains the user-inputted integer. Once the test phrase n!= 0 is converted to 0, the while loop iterates until it returns 0. (false).
- The count is increased by one after the initial iteration, bringing the value of n to 354.
- The number n would be 35 in the second iteration, and the count will have increased by 2.
- The number n will be 3 just after the third iteration, and the count is increased by 3.
- The loop is ended when n reaches 0 at the beginning of the fourth iteration.
- The loop then ends when the test expression is determined to be false.
The application of the abovementioned strategy is seen below:
IntegerExample.java
//this program is for finding the number of digits in a given integer
//import section
import Java.io.*;
import Java.util.*;
public class IntegerExample
{
// method for finding the number of digits in the given integer
public int countDigits(int number)
{
int c = 0;
while(number!= 0)
{
//the last of the given integer is removed for counting the digits
number = number/ 10;
// the value of count(c) is updated to +1
c = c + 1;
}
return c;
}
// main section of the program
public static void main(String argvs[])
{
// the array consisting of integers as an input
int array[] = {780, 19, 22345, 899009, 981, 4214, 3894, 1, 9};
//finding the length of the array
int length = array.length;
// an object (object) is created for the class IntegerExample
IntegerExample object = new IntegerExample();
for(int i = 0; i < length; i++)
{
int c = object.countDigits(array[i]);
System.out.println("The number of digits in the given number "+array[i]+" is--"+c);
}
}
}
Output:

2. By using the String
Another method is making the integer into a string after calculating its length. The string's size determines the size of the string. The very same is seen in the program which follows.
IntegerStringExample.java
//this program is for finding the number of digits in a given integer
//import section
import Java.io.*;
import Java.util.*;
public class IntegerStringExample
{
//method for finding the number of digits in the given integer
public int countDigits(int n)
{
// the given integer is converted to a string
String s = Integer.toString(n);
// computing the size of the string
int len = s.length();
return len;
}
// main section of the program
public static void main(String argvs[])
{
// the array consisting of integers as an input
int array[] = {780, 19, 22345, 899009, 981, 4214, 3894, 1, 90};
//finding the length of the array
int length = array.length;
// an object (object) is created for the class IntegerExample
IntegerStringExample object = new IntegerStringExample();
for(int i = 0; i < length; i++)
{
int c = object.countDigits(array[i]);
System.out.println("The number of digits in the given number "+array[i]+" is: "+c);
}
}
}
Output:

3. By using the Continuous Multiplication
A number 1 can be multiplied by 10 until it exceeds the value of n. When multiplying by 10, we add one to a variable, counting for each time. The count's final value indicates the length of the integer num. Now let us study it with the support of the following Java program.
IntegerMultiplicationExample.java
//This program is for finding the length of the integer in Java
//by using the continuous multiplication method
//import section
//class IntegerMultiplicationExample is created
public class IntegerMultiplicationExample
{
//method for determining the length of the integer
public int countDigits(int num)
{
int temporary = 1;
int c = 0;
while(temporary <= num)
{
//the temporary variable is multiplied to 10
temporary = temporary * 10;
// the value of c is then incremented to 1
c = c+ 1;
}
// the value of c will contain the total number
//of the digits in the given integer number
return c;
}
// main section of the program
public static void main(String argvs[])
{
// user input array
int array[] = {7833, 869, 2345, 9, 1, 401495, 364, 1006, 279};
// the total elements of the array can be calculated using the length function
int len = array.length;
//
IntegerMultiplicationExample obj = new IntegerMultiplicationExample();
for(int i = 0; i < len; i++)
{
int c = obj.countDigits(array[i]);
System.out.println("The number digits in " + array[i] + " is " + c);
}
}
}
Output:

4. By using the Logarithm
Log can also be used to find the integer's length in Java. The program below can be used to understand how can find the integer's length using the log function.
IntegerLogExample3.java
//This program is for finding the length of the integer in Java
//by using the log function in the math module method
//import section
public class IntegerLogExample3
{
//
public int countDigits(int number)
{
// method for determining the length of the integer
int length = (int) (Math.log10(number) + 1);
// the length of the integer can be returned
return length;
}
// main section
public static void main(String argvs[])
{
//the array given by the user as user input
int array[] = {7889, 929, 345, 989, 11, 4074, 7825, 109800, 73627};
// the length of the user input array can be calculated using the length
int len = array.length;
//an object ob for the class IntegerLogExample3 is created
IntegerLogExample3 ob = new IntegerLogExample3();
for(int i = 0; i < len; i++)
{
int c = ob.countDigits(array[i]);
System.out.println("The number of digits in " + array[i] + " is " + c);
}
}
}
Output

5. By using the Recursion method
Recursion is the process in which a function can be called by itself until it has reached a certain condition. For every function call, the allocation of the memory can be done. The concept of recursion in Java can determine the number of the digits in the given integer.
IntegerLengthRecursionExample4.java
//This program is for finding the length of the integer in Java
//by using the concept of recursion
//import section
public class IntegerLengthRecursionExample4
{
// the method count can be used as the Digits in the given integer
public int countDigits(int number)
{
// the base case is checked using the if the condition
if(number/ 10 == 0)
{
return 1;
}
// by the process of recursion, the method can be called
// from the opposite direction, increment the length
return 1 + countDigits(number/ 10);
}
// main section of the program
public static void main(String argvs[])
{
//
int array[] = {498, 967, 345, 9009, 619, 4984, 3874, 7600, 8749};
// the length of the integer can be calculated using the length function
int len = array.length;
// an object ob is created for the class
IntegerLengthRecursionExample4 ob = new IntegerLengthRecursionExample4 ();
for(int i = 0; i < len; i++)
{
int c = ob.countDigits(array[i]);
System.out.println("The number of digits in " + array[i] + " is " + c);
}
}
}
Output:
