Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Recursion Problem Need Help ASAP

  1. #1
    Junior Member
    Join Date
    Jun 2010
    Posts
    24
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Exclamation Recursion Problem Need Help ASAP

    I need help making a recursive program that computes the number of odd digits in a number. This is as far as I have gotten.

    public class ComputeOddNumbers {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
     
       System.out.println("Enter an integer:");
                Scanner input=new Scanner(System.in);
                int number= input.nextInt();
               computeoddnumbers(number); 
               System.out.print("There are "+odds+" odd numbers");
     
    }
    private static int odds;
        public static void computeoddnumbers(int number)
        {
            if(number<10)
            {
            increaseoddscount(number);
            }
            while(number>10)
            {
                increaseoddscount(number%10);
            }
     
     
     
     
        }
        public static int increaseoddscount(int number)
        {
     
     
           if(number==1||number==3||number==5||number==7|number==9)
           {
               odds++;
     
           }
     
     
            return  odds;
            }
     
        }
    Last edited by copeg; June 24th, 2010 at 09:16 PM. Reason: Please use the code tags


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,557
    Thanks
    45
    Thanked 1,953 Times in 1,927 Posts

    Default Re: Recursion Problem Need Help ASAP

    computes the number of odd digits in a number
    I assume you mean how many odd digits in a String of digits. For example "1234" would have 2.
    Or does your number have to be an int?
    Do you have an algorithm or design for this problem? Once you get that, then we can help you when you have problems writing the code.

  3. #3
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,097
    Thanks
    169
    Thanked 779 Times in 725 Posts
    Blog Entries
    5

    Default Re: Recursion Problem Need Help ASAP

    Is there a particular reason this needs to be recursive (as written you do not user recursion)? A few pointers on the code:
    1) Using the modulus operator is very handy to determine odds/evens
    2) Your while loops seems to be infinite when number > 10 ...in loops such as this you must somehow reset the variable you are evaluating for the while.
    3) If you must use recursion, you need a function which calls itself. Your current code does not seem to have this trait.

  4. #4
    Junior Member
    Join Date
    Jun 2010
    Posts
    24
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Recursion Problem Need Help ASAP

    Hey thx.. I changed the code a bit since this post... I have decided to use the modulus operator and I still need a bit of help. And yes this problem does require recursion. Heres the problem from the book. @Norm You are exactly right. If the digit is 1234 then it would output there are 2 odd numbers. This is my first time working with recursion

  5. #5
    Junior Member
    Join Date
    Jun 2010
    Posts
    24
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Recursion Problem Need Help ASAP

    Heres the modified code
    System.out.println("Enter an integer:");
                Scanner input=new Scanner(System.in);
                int number= input.nextInt();
               computeoddnumbers(number); 
               System.out.print("There are "+odds+" odd numbers");
     
    }
    private static int odds;
        public static void computeoddnumbers(int number)
        {
            if(number<10)
            {
            increaseoddscount(number);
            }
            else
            {
     
                {
           int numtemp =number%10;
           increaseoddscount(numtemp);
                }
     
            }
     
        }
        public static int increaseoddscount(int number)
        {
     
     
           if(number%2==1)
           {
               odds++;
           }
     
     
            return  odds;
            }
     
        }
    Last edited by copeg; June 25th, 2010 at 09:41 PM. Reason: Please use the CODE TAGS

  6. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Recursion Problem Need Help ASAP

    Here's a nice little piece of pseudo-code that you can follow to create your code:
    int countOdds(int number)
        If your number is less than 10:
            if it's odd, return 1
            else return 0
        else:
            if number is odd, return 1 + countOdds(number / 10)
            else return countOdds(number / 10)

  7. The Following User Says Thank You to helloworld922 For This Useful Post:

    Delstateprogramer (June 26th, 2010)

  8. #7
    Junior Member
    Join Date
    Jun 2010
    Posts
    24
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Smile Re: Recursion Problem Need Help ASAP

    ...I just knew it was something simple like that....I feel dumb lol, But thanks man preciate it.

Similar Threads

  1. Need a calandar app done asap
    By gixmo in forum Paid Java Projects
    Replies: 6
    Last Post: July 10th, 2010, 08:58 PM
  2. Need simple JAVA program fixing ASAP
    By theviper in forum Paid Java Projects
    Replies: 1
    Last Post: April 14th, 2010, 10:59 AM
  3. Recursion Help
    By vmr in forum Algorithms & Recursion
    Replies: 3
    Last Post: April 1st, 2010, 11:27 PM
  4. [SOLVED] find the position of the field separator in the String---need help ASAP
    By rajesh.mv in forum Java Theory & Questions
    Replies: 6
    Last Post: August 17th, 2009, 10:33 AM
  5. Replies: 1
    Last Post: April 1st, 2009, 02:47 PM