Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com Using a recursion to revere a number

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 11-19-2010, 12:07 AM PM User | #1 yotamoo New to the CF scene   Join Date: Sep 2010 Posts: 8 Thanks: 1 Thanked 0 Times in 0 Posts 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 number, int 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(10, numOfDigits-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
 11-20-2010, 04:57 PM PM User | #2 pigpen Regular Coder   Join Date: Dec 2007 Posts: 137 Thanks: 1 Thanked 21 Times in 21 Posts 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.

 Bookmarks

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 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 Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 10:14 AM.