How to check if a number have even number of digits in Java

1 Answer

0 votes
public class MyClass {
    public static void main(String args[]) {
        int num = 187395;
  
        if ((int)(Math.log10(num) + 1) % 2 == 0)
            System.out.println("Even number of digits");
        else
            System.out.println("Not even number of digits");
    }
}



/*
run:
  
Even number of digits
  
*/

 



answered Sep 8, 2021 by avibootz

Related questions

1 answer 167 views
1 answer 133 views
1 answer 126 views
1 answer 171 views
1 answer 128 views
1 answer 120 views
...