public static long concatenateNumbers(long x, long y) {
long temp = y;
do {
temp = temp / 10;
x = x * 10;
} while (temp > 0);
return (x + y);
}
Example: 230, 400 => 230400
How does this work?
x = 230; y =400
temp = y; => 400
case 1: temp = 400/10 = 40 ; x = 230*10 = 2300
case 2: temp =40> 0; 40/10=4; x = 2300*10 = 23000
case 3: temp =4> 0; 4/10=0; x = 23000*10 = 230000
case 2: temp =0> 0 ; //false.
x + y => 230000+400= 230400
Why do.. while ? why can't while..do ??
Example: 230, 0= > 2300
If i use while(temp> 0) // here itself it will fail, since temp = 0;
How do with java libraries ?
public static long concatenateNumbers(long x, long y) {
retrun Long.parseLong( Long.toString(x)+Long.toString(y));
}
No comments:
Post a Comment