Results 1 to 19 of 19
  1. #1
    Join Date
    Apr 2012
    Posts
    19
    Rep Power
    0

    Default Make a recurisive program that determines whether all digits are even or not...

    I use this method to separate the digits

    a = x / 1000000;
    b = (x - (a * 1000000)) / 100000;
    c = (x - (a * 1000000) - (b * 100000)) / 10000;
    d = (x - (a * 1000000) - (b * 100000) -(c * 10000)) / 1000;
    e = (x - (a * 1000000) - (b * 100000) -(c * 10000) - (d * 1000)) / 100;
    f = (x - (a * 1000000) - (b * 100000) -(c * 10000) - (d * 1000) - (e * 100)) / 10;
    g = (x - (a * 1000000) - (b * 100000) -(c * 10000) - (d * 1000) - (e * 100) - (f * 10)) / 1;
    There is a problem, I can only the separate up to 7 digits, if the number has more than 7 digits, my program won't work, so is there a better way to do so?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default Re: Make a recurisive program that determines whether all digits are even or not...

    Start at the other end and divide by 10. Use % to get the right most digit.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Join Date
    Apr 2012
    Posts
    19
    Rep Power
    0

    Default Re: Make a recurisive program that determines whether all digits are even or not...

    Should I use while loop or for loop?
    Edit: I am thinking for loop, how should I write it?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default Re: Make a recurisive program that determines whether all digits are even or not...

    The type of loop is determined by how you want to end it and if you want index variables.
    When do you want the loop to end?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Join Date
    Apr 2012
    Posts
    19
    Rep Power
    0

    Default Re: Make a recurisive program that determines whether all digits are even or not...

    The loop should end when goes down to the whole number digit?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default Re: Make a recurisive program that determines whether all digits are even or not...

    Can you write a condition that you could use in a while statement to test for that?
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Join Date
    Apr 2012
    Posts
    19
    Rep Power
    0

    Default Re: Make a recurisive program that determines whether all digits are even or not...

    I am suppose to write a Recursive with boolean, but I wrote

    Java Code:
    import javax.swing.JOptionPane; 
    
    public class Recursive_13
    {
        public static void main () {
        String number;
        int x;
        int n;
        
        x = Integer.parseInt(JOptionPane.showInputDialog ( "Enter the a #: " )); 
        
        while (x > 0) {
            n = x%10;
            x = x/10;
        
            if (n%2==0) {
                System.out.print("True");}
            else {
                System.out.print("False");}
        
        }
    }
    }
    I did successfully separate the digits, but the program doesn't tell whether all digits are even or not, I typed in "2478", it printed out "TrueFalseTrueTrue"
    Last edited by Norm; 04-20-2012 at 02:34 AM. Reason: changed quote to code

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default Re: Make a recurisive program that determines whether all digits are even or not...

    the program doesn't tell whether all digits are even or not
    What does the False that was printed out say about the number?
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    Join Date
    Apr 2012
    Posts
    19
    Rep Power
    0

    Default Re: Make a recurisive program that determines whether all digits are even or not...

    The "false" suppose to mean all digits are odd, but in my program (which is wrong), printed out whether the individual digit is odd or not. Not sure how to fix this...
    Last edited by B-52 Stratofortress; 04-20-2012 at 02:39 AM.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default Re: Make a recurisive program that determines whether all digits are even or not...

    false suppose to mean all digits are even
    I not sure I understand what you are saying. That is not how code is written. If an even digit is to print false, then swap the "true" and "false" messages that the code prints.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    Join Date
    Apr 2012
    Posts
    19
    Rep Power
    0

    Default Re: Make a recurisive program that determines whether all digits are even or not...

    Sorry, that's a typo, even should be "True", and odd should be "False", but how should I change the program though?

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default Re: Make a recurisive program that determines whether all digits are even or not...

    how should I change the program
    What do you want the program to do differently?
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    Join Date
    Apr 2012
    Posts
    19
    Rep Power
    0

    Default Re: Make a recurisive program that determines whether all digits are even or not...

    I wish to make a program to determine whether if all digits are even or not. For example, if it's 2478, it should print out "False", instead of "TrueFalseTrueTrue", it should only print out true if all digits are even.

    Edit: I got another program, it says I am missing return statement...

    import javax.swing.JOptionPane;

    public class Recursive_13
    {
    public void init () {
    String number;
    int a;
    a = Integer.parseInt(JOptionPane.showInputDialog ( "Enter the a #: " ));

    areDigitsAllEvenRecursively(a);
    }
    public boolean areDigitsAllEvenRecursively(int x) {
    int n;
    while (x > 0) {
    n = x%10;
    x = x/10;

    if (n%2==0) {
    return true;}
    else {
    return false;}
    }
    }
    }

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default Re: Make a recurisive program that determines whether all digits are even or not...

    If you don't want to see True printed, comment out the line that prints it.
    Have the program set a boolean variable when it prints False.
    At the end of the loop if the boolean variable is not set, print True.

    Your areDigitsAllEvenRecursively() method returns true or false on the first digit it looks at. What about the rest of the digits?
    false is OK, but true should wait until all digits have been looked at.

    missing return statement
    What if the loop never executes?
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    Join Date
    Apr 2012
    Posts
    19
    Rep Power
    0

    Default Re: Make a recurisive program that determines whether all digits are even or not...

    I think I got it, is this correct?

    Java Code:
    import javax.swing.JOptionPane; 
    
    public class Recursive_13
    {
        public void init () {
        String number;
        int a;
        a = Integer.parseInt(JOptionPane.showInputDialog ( "Enter the a #: " )); 
        
        areDigitsAllEvenRecursively(a); 
    }
        public boolean areDigitsAllEvenRecursively(int x) {
        int n;
        while (x > 0) {
            n = x%10;
            x = x/10;
        
            if (n%2!=0) {
                return false;}
        }
        return true;
    }
    }
    The trick is to check out all the "false" first then the "true", because if one digit is odd, then the entire statement is false. Thanks for helping =)

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default Re: Make a recurisive program that determines whether all digits are even or not...

    is this correct?
    A good way to find out is to test the code with different input and see if it does what you want. That's what I'd have to do.

    One thing you might add is a print out of the results.
    If you don't understand my response, don't ignore it, ask a question.

  17. #17
    Join Date
    Apr 2012
    Posts
    19
    Rep Power
    0

    Default Re: Make a recurisive program that determines whether all digits are even or not...

    System.out.print()?

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default Re: Make a recurisive program that determines whether all digits are even or not...

    However you want to report the results to the user.
    If you don't understand my response, don't ignore it, ask a question.

  19. #19
    Join Date
    Apr 2012
    Posts
    19
    Rep Power
    0

    Default Re: Make a recurisive program that determines whether all digits are even or not...

    I got it on my Recurisve one, now I have work on the regular one

    Java Code:
    import javax.swing.JOptionPane;
     
    public class Original_13
    {
        public static void main () {
        String number;
        int x;
        int n;
         
        x = Integer.parseInt(JOptionPane.showInputDialog ( "Enter the a #: " ));
         
        while (x > 0) {
            n = x%10;
            x = x/10;
         
            if (n%2!=0) {
                System.out.print("False"); 
                break;}
        }
        System.out.print("True");
    }
    }
    How do you make the program to go to an end after the printing out "False", because it prints out "FalseTrue" on mine.

    Edit: Wait, nevermind, I used "break;" instead of "return;"...
    Last edited by B-52 Stratofortress; 04-20-2012 at 03:28 AM.

Similar Threads

  1. Replies: 3
    Last Post: 10-27-2011, 05:23 PM
  2. Replies: 0
    Last Post: 03-17-2011, 12:22 PM
  3. Replies: 5
    Last Post: 10-09-2010, 05:22 AM
  4. Replies: 3
    Last Post: 08-19-2009, 03:54 AM

Posting Permissions

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