Reversing Integer, Magic Squares, and LCM problems

Thread Solved

Join Date: Oct 2004
Posts: 6
Reputation: Eclipse is an unknown quantity at this point 
Solved Threads: 0
Eclipse's Avatar
Eclipse Eclipse is offline Offline
Newbie Poster

Reversing Integer, Magic Squares, and LCM problems

 
0
  #1
Oct 29th, 2004
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):

//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:
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!!!
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,590
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 710
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Reversing Integer, Magic Squares, and LCM problems

 
0
  #2
Oct 29th, 2004
>I got the method down here (for the first integer):
Why not write it generally for all integers?
  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:
  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));
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 6
Reputation: Eclipse is an unknown quantity at this point 
Solved Threads: 0
Eclipse's Avatar
Eclipse Eclipse is offline Offline
Newbie Poster

Re: Reversing Integer, Magic Squares, and LCM problems

 
0
  #3
Oct 30th, 2004
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?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,590
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 710
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Reversing Integer, Magic Squares, and LCM problems

 
0
  #4
Oct 30th, 2004
Calculating the least common multiple is trivial if you have a routine to find the greatest common divisor:
  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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 6
Reputation: Eclipse is an unknown quantity at this point 
Solved Threads: 0
Eclipse's Avatar
Eclipse Eclipse is offline Offline
Newbie Poster

Re: Reversing Integer, Magic Squares, and LCM problems

 
0
  #5
Oct 30th, 2004
Thank you so much Narue! You're the best!!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC