Java Program to Count Number of Digits in an Integer

Java Program to Count Number of Digits in an Integer

What is  Integers in Java ?

In Java, an int is a primitive data type that represents an integer value. An integer is a whole number, such as -1, 0, 1, 2, and so on. The int data type is generally used to store small to medium-sized values that do not require a lot of precision.

In this Article, we will write a program to Count Number of Digits in an Integer.

Range of Integers in java

In Java, the int data type is a 32-bit signed integer, which means that it can represent values ranging from -2147483648 to 2147483647. This range is determined by the number of bits that are used to represent the value.

Here is a pseudo code example of how you can use the range of the int data type in Java:

int min = Integer.MIN_VALUE; // -2147483648
int max = Integer.MAX_VALUE; // 2147483647

System.out.println("The minimum value of an int is: " + min);
System.out.println("The maximum value of an int is: " + max);

Note:  The int data type is limited to 32 bits, so it may not be sufficient for storing very large or very precise values. In such cases, you may need to use a different data type, such as long, which is a 64-bit signed integer, or BigInteger, which can represent arbitrarily large integers.

Program to Count Number of Digits in an Integer using While Loop :

Run
import java.util.*;

public class Main {
  public static void main(String[] args) {
      
    // Initializing integers variable
    int num = 12345;
    int count = 0;

    // while loop to count number of digits in an integer
    while (num != 0) {
      num /= 10;
      count++;
    }

    System.out.println("Number of digits: " + count);
  }
}

Output :

Output :

Number of digits: 5

Explanation :

This program uses a while loop to repeatedly divide the input integer by 10 and increment a counter each time. The loop continues until the integer becomes 0, at which point the counter contains the number of digits in the original integer. The result is printed to the console using System.out.println().

Program to Count Number of Digits in an Integer using valueOf() method :

Run
import java.util.*;

public class Main {
  public static void main(String[] args) {
    // Initializing integer variable
    int num = 12345;
    
    // Count number of digits using valueof() method
    int count = String.valueOf(num).length();
    
    // Printing the count
    System.out.println("Number of digits: " + count);
  }
}
Output :

Number of digits: 5

Explanation :

This program uses the length() method of the String class to get the number of characters in a string representation of the integer.
The valueOf() method of the String class is used to convert the integer to a string. This program also prints the number of digits using System.out.println().

Program to Count Number of Digits in an Integer using for Loop :

Run
import java.util.*;

public class Main {
  public static void main(String[] args) {
    // Initializing the integer variables  
    int num = 12345;
    int count = 0;
    
    // for loop to count number of digits in an integer
    for (int i = 0; num != 0; num /= 10, ++count);
    System.out.println("Number of digits: " + count);
  }
}
Output :

Number of digits: 5

Explanation :

This program uses a for loop with an empty initialization and the condition of the loop is num != 0, it will execute the loop until the num becomes zero. Each time the loop iterates, the num is divided by 10 and the count is incremented. After the loop terminates, the count variable contains the number of digits in the original integer, which is printed to the console using System.out.println().

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription