>I got the method down here (for the first integer):
Why not write it generally for all integers?
public int reverse_integer ( int val )
{
int ret = 0;
while ( val != 0 ) {
ret = 10 * ret + ( val % 10 );
val /= 10;
}
return ret;
}
>I am clueless on how to add the other numbers in.
It's easiest just to call reverse for each number, provided that reverse can handle any integer intelligently:
System.out.println("12345 reversed ---> " + fun.reverse(12345));
System.out.println("12345 reversed ---> " + fun.reverse(10001));
System.out.println("12345 reversed ---> " + fun.reverse(1200));
System.out.println("12345 reversed ---> " + fun.reverse(5));
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
Calculating the least common multiple is trivial if you have a routine to find the greatest common divisor:
public int lcm ( int a, int b )
{
return a * b / gcd ( a, b );
}
You shouldn't have any trouble finding out the algorithm for finding the greatest common divisor, it's everywhere. :)
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
I bet the OP has been waiting impatiently for the 7 1/2 years since he posted and then solved this problem, just in case you would come along with another solution. That's great. He can hand his homework in now.
JamesCherrill
Posting Genius
6,370 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073