Largest Digit in a Single Integer Value


SUBMITTED BY: anilkumarsomasi

DATE: May 2, 2016, 7:13 p.m.

FORMAT: Text only

SIZE: 637 Bytes

HITS: 7046

  1. Largest Digit in a Single Integer Value
  2. The following program returns largest Digit in a Single Integer
  3. Ex: Input 7896
  4. Output: 9
  5. public class LargestDigit {
  6. public static void main(String[] args) {
  7. System.out.println(new LargestDigit().getLargestDigit(7896));
  8. }
  9. public int getLargestDigit(int number) {
  10. int max;
  11. if (number / 100 == 0) {
  12. if (number % 10 > (number / 10) % 10) {
  13. max = number % 10;
  14. } else
  15. max = (number / 10) % 10;
  16. } else if (number % 10 > number % 100)
  17. max = number % 10;
  18. else
  19. max = number % 100;
  20. return max / 10;
  21. }
  22. }
  23. Result:
  24. Ouput: 9

comments powered by Disqus