Please help!!! I was able to to print input number without zeros, but I cant get
ID: 3617948 • Letter: P
Question
Please help!!!I was able to to print input number without zeros, but I cant getit quite right to reverse the input number. For example input7080065, should return 5600807. Thank a million.
public class Recursion2{
public static int reverse2(int n) {
int firstDigit;
int lastDigit;
int reset;
int place =(int)Math.pow(10,(int)Math.log10(n));
firstDigit= n/place;
lastDigit =(n % 10);
reset= n % place;
if(n<10)
return n;
return firstDigit + reverse2(reset)*100 + (place/10);
}
public static void main(String[] args){
Scanner kbd = new Scanner(System.in);
int num;
System.out.print("Enter a number: ");
num = kbd.nextInt();
System.out.println(reverse2(num));
}
}
Explanation / Answer
public staticint reverse(int input) { return reverse(input, 0); } privatestatic int reverse(intinput, int output) { if(input == 0) { return output; } else { return reverse(input/10, output*10+input%10); } }