integer - Java reverse an int value without using array -
can explain me how reverse integer without using array or string. got code online, not understand why + input % 10 , divide again.
while (input != 0) {     reversednum = reversednum * 10 + input % 10;     input = input / 10;    }   and how use sample code reverse odd number. example got input 12345, reverse odd number output 531.
i not clear odd number. way code works (it not java specific algorithm) eg. input =2345 first time in while loop rev=5 input=234 second time rev=5*10+4=54 input=23 third time rev=54*10+3 input=2 fourth time rev=543*10+2 input=0
so reversed number 5432. if want odd numbers in reversed number then. code is:
while (input != 0) {         last_digit = input % 10;     if (last_digit % 2 != 0) {              reversednum = reversednum * 10 + last_digit;      }     input = input / 10;  }      
Comments
Post a Comment