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 3 of 3

Thread: recursion problem, please help

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    1
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default recursion problem, please help

    I need to write a code that recieve an integer and find whether it forms an ascending series or not.
    for example:
    123 - true
    2579 - true
    321 - false
    1338 - false
    222 - false
    5 - true

    I wrote this recursion but i don't get the right result
    can someone tell me what's wrong with it please?
    Thanks...

    private static boolean numCheck(int num) {
     
    		if ((num/10) == 0) {
    			return true;
    		} else {
    			if ((num % 10) > ((num/10) % 10)) {
    				numCheck(num/10);
    			} else {
    				return false;
    			}
    		}
     
    	}
    Last edited by helloworld922; October 18th, 2010 at 03:33 PM.

  2. The Following User Says Thank You to nil For This Useful Post:

    javapenguin (October 18th, 2010)


  3. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: recursion problem, please help

    What result do you get instead? What should the algorithm do?

    Post an SSCCE that demonstrates the problem, and we'll go from there.

  4. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: recursion problem, please help

    Quote Originally Posted by nil View Post
    I need to write a code that recieve an integer and find whether it forms an ascending series or not.
    for example:
    123 - true
    2579 - true
    321 - false
    1338 - false
    222 - false
    5 - true

    I wrote this recursion but i don't get the right result
    can someone tell me what's wrong with it please?
    Thanks...


    private static boolean numCheck(int num) {

    if ((num/10) == 0) {
    return true;
    } else {
    if ((num % 10) > ((num/10) % 10)) {
    numCheck(num/10);
    } else {
    return false;
    }
    }

    }
    I can't see anything wrong, unless you're entering negative values.



    123%10 = 3

    123/10 = 12;

    12%10 = 2;

    3 > 2;

    numCheck(12);

    12%10 = 2;

    12/10 = 1;

    1%10 = 1;

    2 > 1;

    numCheck(1);

    1/10 = 0;

    return true;

    122%10 = 2;

    122/10 = 12;

    12%10 = 2;

    2 = 2;

    return false;
    Last edited by javapenguin; October 18th, 2010 at 01:06 PM.

Similar Threads

  1. Recursion
    By javapenguin in forum Algorithms & Recursion
    Replies: 12
    Last Post: October 18th, 2010, 03:42 PM
  2. New Recursion problem need lil help
    By Delstateprogramer in forum Algorithms & Recursion
    Replies: 21
    Last Post: July 8th, 2010, 06:35 PM
  3. Recursion Problem Need Help ASAP
    By Delstateprogramer in forum Algorithms & Recursion
    Replies: 6
    Last Post: June 26th, 2010, 08:36 PM
  4. Recursion Help
    By vmr in forum Algorithms & Recursion
    Replies: 3
    Last Post: April 1st, 2010, 11:27 PM
  5. Recursion help
    By rhoruns in forum Algorithms & Recursion
    Replies: 4
    Last Post: January 8th, 2010, 11:50 PM