Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-18-2010, 11:07 PM   PM User | #1
yotamoo
New to the CF scene

 
Join Date: Sep 2010
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
yotamoo is an unknown quantity at this point
Using a recursion to revere a number

hi guys, i wrote a little recursive method that is supposed to reverse a number so that 123 will be 321. anyway it seems to only work half way. the method parameters are (number,number of digits) and each time it calls itself with new parameters like so -
sending:
(123,3)-->(23,2)-->(3,1)
returning:
3*10-->(30+2)*10-->320+1
(i need to somehow prevent if from multiplying by 10 at the last time)

here's the code:
PHP Code:
    private static int reverse(int numberint numOfDigits) {
        if (
numOfDigits==1) {
            
System.out.println("Returning "+number*10);
            return 
number*10;
        }
        
//factor is used to divide a number like 312 to two numbers: 3 and 12
        
int factor=(int) Math.pow(10numOfDigits-1);
               
// i've added these print commands for control
        
System.out.println("number is "+number+" digits "+numOfDigits);
        
System.out.println("Sending "+number%factor+" digits "+(numOfDigits-1));
        
System.out.println("Returning "+number/factor+reverse(number%factor,numOfDigits-1));
        return 
number/factor+(reverse(number%factor,numOfDigits-1)*10);
    }
    
    public static 
void main(String[] args) {
        
int num=1234;
        
num=reverse(num,4);
        
System.out.println(num);

    } 
and the output, which is weird (it keeps going back and forth!):
PHP Code:
number is 1234 digits 4
Sending 234 digits 3
number is 234 digits 3
Sending 34 digits 2
number is 34 digits 2
Sending 4 digits 1
Returning 40
Returning 340
Returning 40
Returning 2403
number is 34 digits 2
Sending 4 digits 1
Returning 40
Returning 340
Returning 40
Returning 14032
number is 234 digits 3
Sending 34 digits 2
number is 34 digits 2
Sending 4 digits 1
Returning 40
Returning 340
Returning 40
Returning 2403
number is 34 digits 2
Sending 4 digits 1
Returning 40
Returning 340
Returning 40
40321 
i hope i was clear enough, thanx for your help
yotamoo is offline   Reply With Quote
Old 11-20-2010, 03:57 PM   PM User | #2
pigpen
Regular Coder

 
Join Date: Dec 2007
Posts: 137
Thanks: 1
Thanked 21 Times in 21 Posts
pigpen is on a distinguished road
Just change the return statement in your if/conditional statement from multiplying by 1 instead of 10

PHP Code:
        if (numOfDigits==1) { 
            
System.out.println("Returning "+number*10); 
            
//return number*10; 
            // you need to multiply by 1, not 10, for the last iteration of the loop 
            
return number*1
        } 
I ran your code with my change above and it ran fine. The number were correctly reversed.
pigpen is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:32 AM.


Advertisement
Log in to turn off these ads.