[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Finding odd digits in a number in JAVA

Posted on 2012-09-03
7
Medium Priority
?
1,266 Views
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....
0
Comment
Question by:hugonieto
7 Comments
 
LVL 32

Accepted Solution

by:
phoffric earned 668 total points
ID: 38362079
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.
0
 

Author Comment

by:hugonieto
ID: 38362096
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.
0
 
LVL 26

Expert Comment

by:arober11
ID: 38362117
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 31

Assisted Solution

by:GwynforWeb
GwynforWeb earned 668 total points
ID: 38362278
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
else
   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.
0
 
LVL 32

Expert Comment

by:phoffric
ID: 38362290
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.
0
 
LVL 18

Assisted Solution

by:deighton
deighton earned 664 total points
ID: 38363060
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;
    }  
    else
    {
         //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);
     }
   
}
0
 

Author Comment

by:hugonieto
ID: 38366064
Thank you so much guys! I tried it with all of your sugestions and it worked!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Suggested Courses
Course of the Month17 days, 21 hours left to enroll

829 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