Count Number of Digits in an Integer in JAVA programming


  1. import java.util.Scanner;
  2.  
  3.  
  4. public class NummberCount {
  5.  
  6.  
  7. public static void main(String[] args) {
  8.  
  9. Scanner myScanner=new Scanner(System.in);
  10.  
  11. System.out.print("Enter a number : ");
  12.  
  13. long n=myScanner.nextLong();
  14.  
  15. int rem,count=0;
  16.  
  17. while (n!=0) {
  18.  
  19. rem=(int) (n%10);
  20.  
  21. count++;
  22.  
  23. n=n/10;
  24.  
  25. }
  26.  
  27. System.out.print("Number of digits in the entered number is : ");
  28.  
  29. System.out.println(count);
  30.  
  31. }
  32.  
  33.  
  34. }
  35.  
  36. //Enter a number : 3456
  37.  
  38. //Number of digits in the entered number is : 4

No comments:

Post a Comment