Desing an algorithm that accepts a positive integer and reverses the order of its digits.
public class Algorithms {
public static int reverseInt(int n) {
int reversed = 0;
while(n != 0){
reversed = reversed * 10 + (n % 10);
n /= 10;
}
return reversed;
}
}
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;
// TODO: Replace examples and use TDD development by writing your own tests
public class SolutionTest {
@Test
public void reverseIntTest() {
assertEquals(54321, Algorithms.reverseInt(12345));
}
}