×

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:

How to find length of integer in Java

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:

How to find length of integer in Java

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:

How to find length of integer in Java

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

How to find length of integer in Java

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:

How to find length of integer in Java

Related Topics

Banking Application in Java

JDBC (Java Database Connectivity), which provides an API to connect to, execute, and fetch data from any databases, can be used to handle transactions in Java. There are several factors...

7 minutes read.

How to Convert Decimal to Octal in Java

How to Convert Decimal to Octal in Java There are two methods to convert Decimal to Octal: Using toOcatlString() method Using user-defined logic Using Integer.toOctalString() method The toOctalString() is astaticmethod of the Integer...

2 minutes read.

The Diamond Operator in Java 7

The Diamond operator is a comparatively recent Java operator originally developed in JDK 7 to enhance type identification and eliminate boilerplate Java code. The Diamond operator is characterized by a...

2 minutes read.

How to install Java in Windows 10

To make programs that can run on our systems, we need to install the programming language related software in our systems. Different programming language requires a different type of software aka...

6 minutes read.

How Many Ways to Create Objects in Java

Introduction Java comes under the category of object-oriented programming languages. Since it is object-oriented, everything in Java is considered an object. Java is a diverse programming language that is designed to...

6 minutes read.

Skyline Problem in Java

The skyline of a city is the outer edge of the pattern created by all of its structures when viewed from a distance. Return the skyline that these buildings together...

4 minutes read.

How to remove special characters from String in Java

Strings in Java are Objects that are supported inside by a burn exhibit. Since exhibits are immutable (cannot develop), Strings are changeless too. A completely new String is made whenever...

5 minutes read.

How to convert float to String in Java

How to convert float to String in java There are following methods to convert float to String: Convert using Float.toString(float) Convert using String.valueOf(float) Convert using Float.toString(float) The Float class has a static method that returns...

2 minutes read.

How to Create a Package in Java?

For the most part, how to create a package in Java generally, a package is defined as a collection of relevant or irrelevant items together in a very major way....

7 minutes read.

Print Matrix Diagonally in Java

The aim is to print the elements of a matrix of size n*n in some kind of a diagonal pattern. Input : mat[3][3] = {{1, 2, 3},                      {4, 5, 6},                      {7,...

3 minutes read.

Addition Program in Java

Addition Program in Java We can perform the addition (sum) of two or more numbers by using the arithmetic operator (+). To write an addition program in Java, one must understand...

3 minutes read.

Java Break Keyword

Break: The word Break is a keyword or a statement in a java programming language.This Keyword break is used to stop the execution of the loop or switch statements etc.The loop...

3 minutes read.

Getter and Setter Method in Java Example

In Java programming, getter and setter methods are often employed. The values of class fields can be accessed and changed using Java's getter and setter methods. A private access specifier...

6 minutes read.

Jdoodle Java

Everything is instantaneous and fast-paced in the modern world. Online compilers available via the internet are immensely helpful for programmers seeking to learn a new programming language but lacking the...

4 minutes read.

Prime Points in Java

The points that divide an integer into two halves containing a prime number are known as prime points. Printing every prime point of a specific number is the task. Let's...

6 minutes read.

Java Integer compareTo() method

The compareTo() method of Integer class compares two Integer objects numerically. Syntax public static int compareTo(int anotherInteger) Parameters The parameter ‘anotherInteger’ represents the Integer to be compared. Specified by This method is specified by compareTo in...

2 minutes read.

Difference Between Access Specifiers and Modifiers in Java

Java employs access modifiers to restrict a class's data members, member functions, and constructor. Access modifiers are essential when creating Java program and applications. Access modifiers in Java include: defaultpublicprotectedprivate Default Access Modifiers Without...

4 minutes read.

Convert JSON File to String in Java

Before understanding the conversion of JSON file to string, one must know about JSON. What is JSON? JSON stands for JavaScript Object Notation. It is an open standard format lightweight, text-based, and...

3 minutes read.

Java Boolean Keyword

Boolean keyword In java programming language we basically have two types of primitive data types, Boolean and Numeric (integer and floating-point data types). In this article we are going to learn...

4 minutes read.

Java Output Formatting

Sometimes we want a program's output to be displayed in a specific format. The printf () function in the C programming language can be used to achieve this. We will...

4 minutes read.