public class EvenAgainstOdd {
// Write your method here
public static long setEvenAgainstOdd(long num) {
long output = 0;
int power = 0;
while (num > 0) {
num++;
if (num % 2 == 0) {
output = num * ((long) Math.pow(10, power) + output);
power++;
}
}
return output;
}
// Additional test in main
public static void main(String[] args) {
System.out.println(setEvenAgainstOdd(2450667619L)); // 5719240666
}
}
#How did I make an infinite loop
16 messages · Page 1 of 1 (latest)
Hey, @steady plinth!
Please remember to /close this post once your question has been answered!
My prompt: For this challenge, you need to create a public static setEvenAgainstOdd method, which takes a long input (i.e., a possibly very large integer) and returns another long value.
What the method actually does is a little funky: It parses through the digits of its argument and pushes the odd values to the left and, consequently, pushes the even values to the right. The order of the odd and even digits are respected. A couple examples are worth a thousand words.
Input: 123405
Output: 135240
Input: 3045211
Output: 3511042
Only one simple rule: If the method's argument is negative, the sign is switched in the output.
Input: -452
Output: 542
Num is always going to be > 0 so the while loop is infinite
That's not technically correct. It will wrap to negative at some point.
Well, true. But I guess I meant for all intents and purposes