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: HELP Please- Putting Digits within a number in ascending order

  1. #1
    Member
    Join Date
    Mar 2014
    Posts
    92
    My Mood
    Inspired
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default HELP Please- Putting Digits within a number in ascending order

    for example, if i am given 9864
    i must output 4689

    Without use of arrays.


  2. #2
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 63 Times in 61 Posts

    Default Re: HELP Please- Putting Digits within a number in ascending order

    there are many approach with that kind of problem.
    The first approach that I thought is:
    I have int var = 1; int answer = 0;
    then I'll get the first digit, and multiply it by var.
    and then I'll add it to answer; answer now is 9

    next I'll multiply the var by 10, var now is 10
    then I'll get the 2nd digit and multiply it by var, it will be 80 right?
    then I'll add it to answer whose current value is 9, answer now is 89

    next I'll multiply the var again by 10, var now is 100
    then I'll get the 3rd digit and multiply it by var, it will be 600 right?
    then I'll add it to answer whose current value is 89, answer now is 689

    next I'll multiply the var again by 10, var now is 1000
    then I'll get the 4th digit and multiply it by var, it will be 4000 right?
    then I'll add it to answer whose current value is 689,answer now is 4689

    get the logic?

    the other approach is to get each character and store to another string.
    I'll have String answer whose value is "",
    then get the first digit.
    make the answer first digit concatenated by the current value of answer String.
    then get the next digit and make the answer equal to the next digit concatenated by the current value of answer String
    repeat....

  3. #3
    Member
    Join Date
    Mar 2014
    Posts
    92
    My Mood
    Inspired
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: HELP Please- Putting Digits within a number in ascending order

    Thanks

Similar Threads

  1. I get error when putting in more then 10 digits in my vector
    By spirates in forum What's Wrong With My Code?
    Replies: 9
    Last Post: October 15th, 2013, 01:18 AM
  2. ascending order array
    By jamie1234 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 9th, 2013, 06:41 PM
  3. organizing linkedlist in ascending order
    By mia_tech in forum What's Wrong With My Code?
    Replies: 7
    Last Post: June 9th, 2012, 06:44 AM
  4. Putting codes in order......
    By rdmighty in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 1st, 2012, 11:51 AM
  5. Replies: 3
    Last Post: October 22nd, 2011, 01:53 AM