Results 1 to 3 of 3

Thread: Recursion in java

  1. #1
    Join Date
    Jul 2009
    Posts
    127

    Recursion in java

    Hi
    I am trying to write an static recursive method that returns the occurrence a particular digit d in an integer n. For example, if passed (1342457,4) it should return 2, whereas when passed (1342457,6) it should return 0. This is not my actual code and it will not sound like that, I just want help with the logic in this. I can't grasp what needs to be done. i should also take care of that no variables are other than the two that the user will input as the integer, and the digit to be checked for.

  2. #2
    Join Date
    May 2008
    Posts
    2,302

    Re: Recursion in java

    Hi
    I think you need to use at least one more variable. I think you should read the integer in as a string. Create an array of all it's char's and then compare each one to the digit. Hope this will solve your problem. If not do post back again.

  3. #3
    Join Date
    Jul 2009
    Posts
    127

    Re: Recursion in java

    Hi
    Thanks for replying. I will take care of what you have said. I am posting my code below see if you can help me.
    Code:
    // Returns the largest element in an integer array
    public static int maxa(int[] a, int size)
    {
    if (size==1)
    return (a[0]);
    else
    {
    int temp = maxa(a, size-1);
    if(temp>a[size-1])
    return temp;
    else
    return a[size-1];
    }
    }

Similar Threads

  1. Replies: 4
    Last Post: 04-09-2013, 11:04 PM
  2. Iteration and recursion in C++ language
    By Kim|ball in forum Software Development
    Replies: 4
    Last Post: 27-06-2011, 10:32 AM
  3. How to Disable Recursion on the DNS Server
    By Aliki in forum Networking & Security
    Replies: 5
    Last Post: 12-12-2010, 06:36 PM
  4. Link List Example in Java Sample program in Java
    By trickson in forum Software Development
    Replies: 2
    Last Post: 04-08-2009, 08:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •