answersLogoWhite

0

Please note that the answer below is the most expandable answer. By this I mean that it can be easily changed to display the output given for any number of digits in a number. There are far simpler solutions which do not require any explanation, but are useless in an evolving setting.

To answer this question, we need to look at the Math.log10 function. Ignoring the possible anomalous results of this function (check the current Java API to find out what other results are possible), we can find the number of digits in a number by using: (log10(n) + 1). If n is equal to the value 10^m for a value m, then the function will return m. That is to say, if n is equal to 10, then the result of the function will be 1, since 10^1 = 10. Likewise, if n is equal to 100, then the result of the function will be 2, since 10^2 = 100, and so forth. If you look at the numbers in between, you will find the function returns a decimal result. log10(99) = 1.9956, for example. This means that the range of answers you will get for the values [10,99] will return the range of answers from [1.0,2.0). The same can be found looking at the range of values [100,999] which return in the range [2.0,3.0). From this we find that the function log10 always returns either an integer or a fractional value which will always be at most 1.0 less than the number of digits in the number sent to it. In either case, if we simply truncate it (cast the double as an int) and add 1, we come up with our getNumDigits function.

int getNumDigits(int n) {

return ((int) Math.log10(n)) + 1;

}

And to finish up your question, we can add in another method to deal with the results.

// we're assuming the number n comes from somewhere else,

// whether hard-coded or passed as input to the program

void displayNumDigits(int n) {

switch(getNumDigits(n)) {

case 1:

System.out.println("One digit");

break;

case 2:

System.out.println("Two digits");

break;

case 3:

System.out.println("Three digits");

break;

default:

System.out.println("More than three digits");

}

}

User Avatar

Wiki User

16y ago

What else can I help you with?

Continue Learning about Engineering

How do you find out how many digits are in integer using vbscript?

num=32767 MsgBox(len(num))


How do you show a positive integer for example 12345678901234567890 in C sharp that has more than 17 digits?

There is Int64 class, it will do it.


In java How do you have a method take in an int value and return an int containing sum of the individual digits in the number using a recursive method?

Add the last digit plus the sum of all the previous digits. The base case is that if your integer only has a single digit, just return the value of this digit. You can extract the last digit by taking the remainder of a division by 10 (number % 10), and the remaining digits by doing an integer division by 10.


Individual digits sum with flowchart and algorithm?

Well, it's very hard to write a flowchart in text, so I'll give you some pseudo code instead. int number = the given number int sum = 0 loop while number is not 0 sum = sum + (number mod 10) number = number / 10


How do you extract digits from an integer in java?

There are different ways to do it. One is to convert it to a String, then use the string manipulations methods to extract individual digits as strings. You can then convert them back to numbers. Another is to do some calculations. For example, to get the last digit: int i = 12345; int lastdigit = i % 10; //To get additional digits, divide by 10 and repeat: i /= 10; int lastdigit = i % 10; In this case you can create a loop for this (repeating while i > 0), and copy the digits to an array.

Related Questions