0

The challenge is to remove any Zeros at the end of the number. Zeros within a two number are okay. EG:

14000 == 14 //all end zeros removed
10300 == 103 // all end zeros removed

I wrote this function to solve the problem:

    public class NoBoring {
    public static int noBoringZeros(int n) {
      System.out.println(n); // testing 
      System.out.println(Math.abs(n % 10)); //testing
        if(Math.abs(n % 10) != 0){
          return n;
        }
      return noBoringZeros(n/10);
    }
}

Unfortunately this doesnt work for negative inputs. See my output of the test case below:

//LD = last digit of the number
//ZR is the zero removed
        Fixed Tests: noBoringZeros
    1450
    0 //LD
    145 //ZR
    5 //LD
    960000 
    0 //LD
    96000 //ZR
    0 //LD
    9600 //ZR
    0 //LD
    960 //ZR
    0 //LD
    96 //ZR
    6 //LD
    1050
    0 //LD
    105 //ZR
    5 //LD
    -1050
    0 //LD
    -105 //ZR
    5 //LD
    -105 
    5
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0

I dont understand fully why this is failing? I would have thought the drop out statement of n % 10 not = 0 would cause the return of n but it doesnt seem todo that for negative numbers?

ollie
  • 255
  • 1
  • 2
  • 15
  • 1
    This might help you: http://stackoverflow.com/questions/4403542/how-does-java-do-modulus-calculations-with-negative-numbers – RaminS Apr 21 '16 at 19:05
  • 1
    [I don't think the issue is with negative numbers](http://ideone.com/i09F8T) but is actually when entering `0`, since `0 / 10 = 0`, so it will continue forever. – Obicere Apr 21 '16 at 19:13
  • @Gendarme im not too sure on how to implement that, i tried the int r = n % 10. then setup another if statement: if (r > 0 && n < 0){ return r -= 10; } But ti doent work - same output. – ollie Apr 21 '16 at 19:15
  • @Obicere Thanks thats the problem i wasnt checking for n == 0 and droping out. thanks. – ollie Apr 21 '16 at 19:17

3 Answers3

1

If you are not comfortable with Math functions , you can use String fucntions:

     int n = -22400 ; // You want -224           
     String str = String.valueOf(n);

     while(str.endsWith("0")){

            if (str != null && str.length() > 0 && str.charAt(str.length()-1)=='0') {

                 str = str.substring(0, str.length()-1);
            }

        }

     System.out.println(Integer.parseInt(str));
Tilak Madichetti
  • 4,110
  • 5
  • 34
  • 52
1

Your code has problems with negative numbers and zero as inputs. One solution is to deal with them separately. This is in pseudocode, just to make sure you do some of the work yourself:

function void noBoringZeros(int n)

  if (n < 0) then

    // Deal with negative n.
    print "-" + nbz(-n)

  else if (n = 0) then

    // Deal with zero n.
    print 0  // or "All zeros" or whatever.

  else

    // Deal with positive n.
    print nbz(n)

  end if

end function

Here nbz() is a simple function that strips trailing zeros off a positive number:

function int nbz(int n)

  // n >= 1 assumed.
  int temp <- n
  while (temp MOD 10 = 0)
    temp <- temp DIV 10
  end while

  return temp

end function
rossum
  • 15,344
  • 1
  • 24
  • 38
  • 1
    Usually challenge problems like these enforce it to be done a certain a way. So the `while` loop may make this answer be inappropriate as a solution. – Obicere Apr 22 '16 at 01:21
  • 1
    The loop in `nbz()` can be turned into a recursion if that is needed (the question title mentions recursion). Best to leave some work for the OP to do. – rossum Apr 22 '16 at 08:58
0
    public class NoBoring {
    public static int noBoringZeros(int n) {
      System.out.println(n);
      System.out.println(Math.abs(n % 10));

        if(Math.abs(n % 10) != 0 || n == 0){
          return n;
        }

      return noBoringZeros(n/10);
    }
}

didnt check for n == 0;

ollie
  • 255
  • 1
  • 2
  • 15