java - How do i find the highest digit in a multi-digit number? -
OK, so I have an exercise, I need to find the highest and lowest digits in any number and add them together . So I have a number n which is 5368 and the code is to find the highest (8) and the lowest (3) number and add them together (11). How do I do this? I have tried to do something like this:
public class class {public static void main (string [] args) {// TODO Auto-generated method stub int n1 = 5; Int n2 = 3; Int N3 = 6; Int n4 = 8; Int max = Math.max (n2, n4); Int min = Math.min (n2, n4); Int zodiac = max + min; Println (zodiac); }}
Which works, but I have a 4 digit number and with Math.max / min, I can only use 2 arguments. How do i do this thank you in advanced.
I think that intent is to do this with n = 5368 so that you It will require a person to draw a loop and compare it with the current minimum / maximum
int n = 5368; Int results = 0; If (n> gt; 0) {int min = Integer.MAX_VALUE; Int max = integer MIN_VALUE; While (n> gt; 0) {int number = n% 10; Max = Math Maximum (max, digits); Minimum = Math Minutes (minimum, number); N / = 10; } Results = min + max; } System.out.println (results);
Comments
Post a Comment