for example, if i am given 9864
i must output 4689
Without use of arrays.
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.
for example, if i am given 9864
i must output 4689
Without use of arrays.
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....
Thanks