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

Thread: Pin code generator with recursion

  1. #1
    Junior Member
    Join Date
    Aug 2019
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Pin code generator with recursion

    Hello, please help me with my home work...

    Create program which generates pin codes (allowed numbers 1-9), the user inputs length of the code and the biggest number
    This has to be done with recursion

    So the program would be:

    Length: 3
    biggest number: 2

    111
    112
    121
    122
    211
    212
    221
    222

    Length: 5
    biggest number: 4

    11111
    11112
    11113
    11114
    11121
    ...
    44444

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Pin code generator with recursion

    What have you tried?

    Be sure to wrap your posted code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Aug 2019
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Pin code generator with recursion

    Hello Norm,

    my first approach was to create an array[length] and fill it with 1, then call a method which adds recursively every index up until the highest number, that would've been too easy...
    then I got maybe the craziest idea ever, to look for the patterns and try to create a mathematical approach to it... way too complicated...
    then I tried to create a method which calculates combinations, store them in an array and then sort the array. But that didn't work either.
    Last edited by Mironoff; August 18th, 2019 at 05:10 AM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Pin code generator with recursion

    The desired output looks like a series of incremented numbers in a different base and first digit. Instead of base 10 and with starting digit of 0 the base is the "biggest number" + 1 and the starting digit is 1.
    So starting with 111 base 2+1 you get the first series you posted by adding 1 to the number until the max of 222 is reached.
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    Mironoff (August 19th, 2019)

  6. #5
    Junior Member
    Join Date
    Aug 2019
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Pin code generator with recursion

    Thanks a million Norm! I was blind I try that right away.
    Last edited by Mironoff; August 19th, 2019 at 03:31 AM.

Similar Threads

  1. recursion code seems to keep running with no end (halting problem?)
    By Helplessdrowningpuppy in forum Loops & Control Statements
    Replies: 1
    Last Post: October 6th, 2014, 04:37 AM
  2. After a number of pin attempts it will end the loop
    By kulotz in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 3rd, 2013, 05:37 PM
  3. basic recursion problem - code needs to be modified
    By ash12 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 6th, 2012, 08:20 AM
  4. recursion code
    By Java_boy in forum Algorithms & Recursion
    Replies: 8
    Last Post: May 27th, 2012, 11:12 AM
  5. Replies: 0
    Last Post: January 25th, 2011, 01:24 AM