JAVA PROGRAM TO COUNT DIGITS IN AN INTEGER

In this module we will learn how to count digits in a number entered by the user, we will see different approches regarding the solution.

Table of Contents

[hide]
  • Java Program to count digits in an Interger (Iterative solution).
  • Java Program to count digits in an Interger using for loop.
  • Java Program to count digits in an Interger using logarithm.
  • Java Program to count digits in an Interger using recursion.
  • Java Program to reverse a given Number

Example 1 : Java Program to count digits in an Interger (Iterative solution)

Algorithm

  • Take input from the user and store it in variable name num.

  • Pass this number to a method name countdigit() which we can acces by the obj of CountDigits class.

  • A while loop is being used to iterate the text expression until n!=0

  • The countdigit() method will return the local variable result which initially is assigned to 0 and keeps on increasing by 1 until n!=0.

  • Finally print the count inside the main method as shown in the given program below.

copyimage
// Java Program to calculate the number of digits in an integer
import java.util.Scanner;
class CountDigits {
     public int countdigit(int n) {
		
     int result = 0;
     while (n != 0) {
       n = n / 10;
       result++;
     }
       return result;
   }

}

public class Codebator {
    public static void main(String[] args) {
       CountDigits obj = new CountDigits();
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter any number");
       int num = sc.nextInt();
       int result = obj.countdigit(num);
       System.out.println("Digit count is "+result);
    }
}

OUTPUT :

Enter any number523456 Digit count is 6 

Example 2 : Java Program to count digits in an Interger using for loop

copyimage
// Java Program to calculate the number of digits in an integer
import java.util.Scanner;
class CountDigits {
     public int countdigit(int n) {
		
     int result = 0;
     for(;  n!=0 ; n=n/10,result++) {
     }
    return result;
   }

}

public class Codebator {
    public static void main(String[] args) {
       CountDigits obj = new CountDigits();
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter any number");
       int num = sc.nextInt();
       int result = obj.countdigit(num);
       System.out.println("Digit count is "+result);
    }
}

OUTPUT :

Enter any number6565676 Digit count is 7 

In each iteration the value of n is being divided by 10 and the value of result is being incremented by 1. The loop exits one n!=0 i.e num=0;

Example 3 : Java Program to count digits in an Interger using logarithm

copyimage
//Java Program to calculate the number of digits in an integer
import java.util.Scanner;
class CountDigits {
  public int countdigit(int n) {	
      return (int)Math.floor(Math.log10(n) + 1);
  }
}

public class Codebator {
 public static void main(String[] args) {
    CountDigits obj = new CountDigits();
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter any number");
    int num = sc.nextInt();
    int result = obj.countdigit(num);
    System.out.println("Digit count is "+result);
 }
}

OUTPUT :

Enter any number98767875 Digit count is 8 

Example 4 : Java Program to count digits in an Interger using recursion

copyimage
//Java Program to calculate the number of digits in an integer
import java.util.Scanner;
class CountDigits {
   public int countdigit(int n) {
      if(n==0)
         return 0;
      return 1+countdigit(n/10);
   }
}

public class Codebator {
  public static void main(String[] args) {
     CountDigits obj = new CountDigits();
     Scanner sc = new Scanner(System.in);
     System.out.println("Enter any number");
     int num = sc.nextInt();
     int result = obj.countdigit(num);
     System.out.println("Digit count is "+result);
  }
}

OUTPUT :

Enter any number34564 Digit count is 5