944,044 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 11727
  • Java RSS
Oct 29th, 2004
0

Reversing Integer, Magic Squares, and LCM problems

Expand Post »
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):

Quote ...
//initialize
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:
Quote ...
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!!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Eclipse is offline Offline
6 posts
since Oct 2004
Oct 29th, 2004
0

Re: Reversing Integer, Magic Squares, and LCM problems

>I got the method down here (for the first integer):
Why not write it generally for all integers?
Java Syntax (Toggle Plain Text)
  1. public int reverse_integer ( int val )
  2. {
  3. int ret = 0;
  4.  
  5. while ( val != 0 ) {
  6. ret = 10 * ret + ( val % 10 );
  7. val /= 10;
  8. }
  9.  
  10. return ret;
  11. }
>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:
Java Syntax (Toggle Plain Text)
  1. System.out.println("12345 reversed ---> " + fun.reverse(12345));
  2. System.out.println("12345 reversed ---> " + fun.reverse(10001));
  3. System.out.println("12345 reversed ---> " + fun.reverse(1200));
  4. System.out.println("12345 reversed ---> " + fun.reverse(5));
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 30th, 2004
0

Re: Reversing Integer, Magic Squares, and LCM problems

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Eclipse is offline Offline
6 posts
since Oct 2004
Oct 30th, 2004
0

Re: Reversing Integer, Magic Squares, and LCM problems

Calculating the least common multiple is trivial if you have a routine to find the greatest common divisor:
Java Syntax (Toggle Plain Text)
  1. public int lcm ( int a, int b )
  2. {
  3. return a * b / gcd ( a, b );
  4. }
You shouldn't have any trouble finding out the algorithm for finding the greatest common divisor, it's everywhere.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 30th, 2004
0

Re: Reversing Integer, Magic Squares, and LCM problems

Thank you so much Narue! You're the best!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Eclipse is offline Offline
6 posts
since Oct 2004
18 Days Ago
0

reversing

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

}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
k.chinnivasu is offline Offline
1 posts
since Jan 2012
18 Days Ago
1
Re: Reversing Integer, Magic Squares, and LCM problems
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.
Featured Poster
Reputation Points: 1924
Solved Threads: 952
Posting Expert
JamesCherrill is online now Online
5,804 posts
since Apr 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Convert pdf file to HTML in Java
Next Thread in Java Forum Timeline: Building a GUI





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC