Computer problems,Computer help
*AX SOFT>>>Programming & Design

How do you solve the following problem recursively?


I was wondering how to solve the following problem:

Write a Java method 'printAlternateDigits' that takes an integer as a parameter and prints every alternate digit, ending at the rightmost digit. No credit will be given for non-recursive implementation. No copying to arrays or strings is allowed.

Example Input and Output:
45452 returns 442
643634 returns 4
456 returns 46
67 returns 7
7 returns 7

So? Any ideas. Here's how I had started it out earlier:

public static void printAlternateDigits (int num)
{
// Base Case
if ((int)(num/100)==0)
System.out.println(num - (int)(num/10)*10);
// Recursion
else
printAlternateDigits(umm...wtf? I'm lost!!);
}

Please help me out. This actually isn't an assignment. It was a test question that I had yesterday and it screwed me over, and I'm pissed. I think this problem was too hard for the first exam.

Anyway, thanks for your help guys!

I'm not familiar with Java functions (you need mod and a print statement that doesn't add a newline) . Formatting is probably a little screwed up...want it in python?

public static void printAlternateDigits( int num)
{
if num>0
{
myPart = num % 100 // or is it mod(num,100)
printAlternateDigits( (num-myPart)/100 )
System.out.println(myPart %10)
}
}

by mod I mean:

12345 % 100 = 45

Tags
  General - Computers & Internet   Software   Security   Programming & Design   Facebook   Flickr   Google   MSN   MySpace
Related information
  • Adobe AIR...just another widget platform?

    Because Adobe doesn't have any Widgets or gadget or konfabulator of it's own. This is a new trend and they want to get in on it. Simple as that.

    ...
  • I downloaded one .nrg file. How do i access it?

    Its actually a nero image file.. An image file is file that has all the files in a cd compiled in it. u need a virtual drive to mount this .nrg file, so that u can use it the best virtual ...

  • Is there a calls limit for Google AJAX search API and Youtube API?

    There is no call limit, however be aware that all AJAX web request take some time to process the request and return a response. You must allow some sort of small wait state or waiting mechanism wi...

  • C++ Leap Year program with "variable is being used without being initialized" and always return true problem

    I think there are two problems in your code. First, the warning is talking about this function char moreData(){ char carryOn; cout<<"Do you want to enter more data? (y/n) "; ...

  • How do i store data in order from visual basic6 to MS access?

    Do you have an index on this table? If not, create one in Access or use "select * from table order by Field1" as your adodc source.

    ...
  • Why is this javascript not working in FireFox? Nothing shows.?

    I've added a statement after the line function randomlyselectMessage1() { --------------------------------------... var Message1 = document.getElementById( 'Message1' ); -----...

  • If computer are ones and zeros how do sounds and moving images work?

    I want to write a huge answer. I'll try to keep it short. The wonderful variety of media you are used to seeing is simply the result of very complex and numerous sets of these 0's and...

  • Need help calculating in access?

    First off, you don't need [order line id] field in your order table: in fact you don't need [order line id] at all: you can create a combined key on [order id] and [item id]. Design a ...

  •  

    Categories--Copyright/IP Policy--Contact Webmaster