java - Recursion- Kata challenge -
the challenge remove zeros @ end of number. zeros within 2 number okay. eg:
14000 == 14 //all end zeros removed
10300 == 103 // end zeros removed
i wrote function solve 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 doesnt work negative inputs. see output of test case below:
//ld = last digit of number //zr 0 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 why failing? have thought drop out statement of n % 10 not = 0 cause return of n doesnt seem todo negative numbers?
if not comfortable math functions , can use string fucntions:
int n = -22400 ; // 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));
Comments
Post a Comment