Computer Applications

Raj wanted to count the number of digits in a given number without using a loop. Which of the following statements is correct to perform the above?

  1. String.valueOf(n).length()
  2. Integer.parseInt(n).length()
  3. n.length()
  4. All the above

Java String Handling

3 Likes

Answer

String.valueOf(n).length()

Reason — The expression String.valueOf(n).length() converts the number n into a string using String.valueOf(n), and then calculates the number of characters (digits) in that string using .length(). This is a valid and loop-free way to count digits in an integer.

Answered By

1 Like


Related Questions