public class HelloWorld{
public static void main(String []args){
int orig=103, reverse=0, mod;
int numOfDigits=0;
int n = orig;
while (n>0){
n /= 10;
numOfDigits++;
}
n = orig;
while (n > 0){
mod = n % 10;
reverse = reverse + (int)(mod * java.lang.Math.pow(10, numOfDigits-1));
numOfDigits--;
n /= 10;
}
System.out.println("Reversed is : " + reverse);
}
}
I do know that reverse = reverse + (int)(mod * java.lang.Math.pow(10, numOfDigits-1));
can be replaced by reverse = mod + (reverse*10)
.
Was wondering if I had just increased the complexity of a simple program by calculating total number of digits and applying power?
P.S: Kindly assume that orig can be taken as an input from the user and could be a number of any number of digits. I have hard coded only for experiment.