| | |
Reversing Integer, Magic Squares, and LCM problems
Thread Solved |
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):
However, there seems to be a problem displaying it in the output, which looks like this:
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!!!
•
•
•
•
//initialize
int tmpInt1 = 0;
while(tmpInt1 == 12345)
{
newInt *= 10;
newInt += tmpInt1 % 10;
tmpInt1 /= 10;
}
•
•
•
•
FunLoops fun = new FunLoops();
System.out.println("12345 reversed ---> " + fun.reverse(12345));
>I got the method down here (for the first integer):
Why not write it generally for all integers?
>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:
Why not write it generally for all integers?
Java Syntax (Toggle Plain Text)
public int reverse_integer ( int val ) { int ret = 0; while ( val != 0 ) { ret = 10 * ret + ( val % 10 ); val /= 10; } return ret; }
It's easiest just to call reverse for each number, provided that reverse can handle any integer intelligently:
Java Syntax (Toggle Plain Text)
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));
I'm here to prove you wrong.
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?

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:
You shouldn't have any trouble finding out the algorithm for finding the greatest common divisor, it's everywhere.
Java Syntax (Toggle Plain Text)
public int lcm ( int a, int b ) { return a * b / gcd ( a, b ); }
I'm here to prove you wrong.
![]() |
Similar Threads
- switch, and while loop outputting integer numbers and their squares (C++)
- magic square assignment..need help plz (C++)
- Magic Square - Error Message (C++)
- Vector of vectors & magic square issues (C++)
- problem with program finding "magic squares" (Java)
Other Threads in the Java Forum
- Previous Thread: Help Needed Urgently With Coding
- Next Thread: Please help me whit this java question.
| Thread Tools | Search this Thread |
actuate add android api applet application applications array arrays automation balls bank binary bluetooth business chat class clear client code codesnippet collections component database defaultmethod development dice digit dragging ebook eclipse equation error event formatingtextintooltipjava fractal functiontesting game givemetehcodez graphics gui health hql html hyper ide idea image infinite int integer invokingapacheantprogrammatically j2me java javame javaprojects jni jpanel julia linux list main map method methods mobile myregfun mysql netbeans nonstatic openjavafx parameter pearl php problem program project recursion repositories scanner scrollbar server set sms sort sorting spamblocker sql sqlserver state storm string sun superclass swing swt thread threads tree windows







