Finding odd digits in a number in JAVA

Posted on 2012-09-03
Last Modified: 2012-09-04
Hello guys!

I'm trying to write a recursive method that computes the number of digits in a number with JAVA code. I need some help on how to start or how to write it..... I know basic java but Im lost on how to make this happen....
Question by:hugonieto
    LVL 31

    Accepted Solution

    Here's the math part:
    n = 12345
    n/10 = 1234 (integer truncation)
    (n/10)*10 = 12340
    n - 12340 = 12345 - 12340 = 5, odd because 5 mod 2 == 1

    Now call the recursive method again with 1234 and keep count of number of odd digits.

    Author Comment

    Thanks phoffric!

    I will try it.... I know how to do this with a for loop or any other loop but with a recursive method..... I'm little lost! But thank you for the help! I will try to write your suggestion.
    LVL 26

    Expert Comment

    LVL 31

    Assisted Solution

    In JAVA integer division removes the last digit and n%10 (ie n mod10) gives you the last digit.
    Also n%2 =0 if n =even,  n%2=1 if n is odd.

    So recursively the function numOddDigits(n) mathematially  is given by

    if n/10 = 0
       numOddDigits(n)  = n%2   //ie n is a single digit
       numOddDigits(n)  =    numOddDigits(n/10)  + (n%10)%2

    Now you have to code that mathematical definition up as a recursive Java function

    I have given you a start.
    LVL 31

    Expert Comment

    I tried to help with the algorithm. May I suggest that once you understand the algorithm that you write up what you can (even if not recursive), and post the problem code in the Java zone. That is a good place to get specific coding advice.
    LVL 18

    Assisted Solution

    sorry it is c (untested!) but treat it as pseudo code.

    int odd_digits(int x)

        int next;
        int odd;
        next = x / 10;

        //mod operation, as suggested by previous posts
        odd = (x - next * 10) % 2;

        if (next == 0)
             return odd;
             //Recursive function call, add the odds we counted here (0 or 1) to the count for the rest of the string
             return odd + odd_digits(next);

    Author Comment

    Thank you so much guys! I tried it with all of your sugestions and it worked!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    Prime numbers are natural numbers greater than 1 that have only two divisors (the number itself and 1). By “divisible” we mean dividend % divisor = 0 (% indicates MODULAR. It gives the reminder of a division operation). We’ll follow multiple approac…
    Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
    Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
    Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now