J
J
Jony13372016-12-05 14:53:52
Java
Jony1337, 2016-12-05 14:53:52

How to print the largest digit from a number in java?

Let's say there is a number n= 7894514 , you need to display 9 , this is the largest number that is in the number. How to do?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mercury13, 2016-12-05
@Mercury13

Take it apart and do it.
A positive number is parsed by digits like this.

while (n != 0) {
  digit = n % 10;  // делай с этой цифрой что хочешь
  n /= 10;
}

Where to put the numbers and how to make corrections for zeros and negatives - think for yourself.

A
Alexander Kosarev, 2016-12-06
@jaxtr

Arrays.stream("7894514".split("(?!^)"))
                .mapToInt(Integer::parseInt)
                .max()
                .getAsInt()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question