Hello. I'm currently a 10th grader who's been sitting in front of the computer for hours trying to figure this assignment out. First, my teacher gave me a reverse integer problem. He wants us to reverse the numbers 12345, 10001, 1200, and 5, and display in in the output. The leading zeros can be omitted. I got the method down here (for the first integer):

    int tmpInt1 = 0;

    while(tmpInt1 == 12345) 
        newInt *= 10;
        newInt += tmpInt1 % 10;
        tmpInt1 /= 10;

However, there seems to be a problem displaying it in the output, which looks like this:

    FunLoops fun = new FunLoops();
    System.out.println("12345 reversed ---> " + fun.reverse(12345));

That's just the first reverse integer, and I am clueless on how to add the other numbers in. Should I use a nested loop? If so, how should I use it? After I figure this out, I still need to do the LCM and the Magic Square part, which is even more complicated. I'm glad I have a 4-day weekend, so this isn't that urgent. But I still need the help. Can someone please show me the correct way to do this? Thank you all so much!!!

Recommended Answers

All 6 Replies

>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));

Thank you, Narue! This portion of the program is working now! :)

Hopefully, I'm not asking too much, but there's just one last thing I need, and that is how to figure out the LCM of two numbers. I don't really know how to approach it. Should it be best if I stick with using a while loop, compare the two integers, and increment it until it finds the LCM, or should I do something else?

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. :)

Thank you so much Narue! You're the best!! :cool:

class ReverseTest {

public static void main(String[] args) {

int originalInt = 12345;
int reversedInt = 0;

// convert Integer value to String. Strings can be easily reversed
String intToString = new Integer(originalInt).toString();

//Create a StringBuffer from the original string
StringBuffer buffer = new StringBuffer(intToString);

//Reverse the contents of the StringBuffer
buffer = buffer.reverse();

// convert String back to integer
reversedInt = Integer.parseInt(buffer.toString());

// print out the result
System.out.println("Reversed Integer: " + reversedInt);

} // main


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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.