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

# Thread: Need some math help

1. ## Need some math help

Hidy ho everyone,

I found this code on how to break apart a number and store it in an array online. Quite frankly, I still don't understand how it works.

```	int input = Integer.parseInt(JOptionPane.showInputDialog("Please enter a 4 digit number"));

int[] numbers;

numbers = new int;

for (int i = 3; i > -1; i--)
{
numbers[i] = (input % 10);
input = input /10;
}```

now I'm using this code again in another homework program where the user has to input a 4 digit number and I have to break that number apart. This program is about encryption.

What I have to do is replace each digit with the result of adding 7 to the digit and getting the remainder after dividing the new value by 10. Then swap the first digit with the third, and swap the second digit with the fourth. Then print the encrypted integer (copied and pasted from my ebook text).

now I can add 7 to everything in the array, but I don't understand what its asking me to do with getting the remainder after dividing by 10. That's where I'm having my difficulty.  Reply With Quote

3. ## Re: Need some math help

You question is how to find the remainder of dividing by 10? Refer to the modulus operator.  Reply With Quote

4. ## Re: Need some math help

Wait, is that what I was doing already with my first for loop?  Reply With Quote

5. ## Re: Need some math help

Did you try it? What happened?  Reply With Quote

6. ## Re: Need some math help

i'm not sure. up in my first post I have a for loop with a % sign as the arathmetic operator. And a cursory google search shows that as the way to finding the remainder by dividing by 10.

Is that what that does?  Reply With Quote

7. ## Re: Need some math help

To see how to use the modulus operator, write a very small program that uses it and print out the results. Experiment some by changing the numbers and running it again. Try it several times to see what happens.  Reply With Quote

8. ## Re: Need some math help

Trouble is, I don't understand the math behind it. Those google searches haven't helped that.  Reply With Quote

9. ## Re: Need some math help

5 % 10 = 5
12 % 10 = 2
27 % 10 = 7

What do you not understand about "the math behind it"? A remainder is a remainder.  Reply With Quote

10. ## Re: Need some math help

Did you write the test program I suggested so you could see the results with different numbers?
Greg has provided you with several samples of what happens.  Reply With Quote

11. ## Re: Need some math help

oh, so its just a way of separating the right most number from the left most?

edit: also, i'm not quite sure what to write for that test program.

Would it be something like:

int input, remainder

scanner.in.input

remainder = input % 10

println("Input: " + input)
println("Remainder: " + remainder)

that's probably a lot of wrong code. I was just spit balling.

2nd Edit:

I think I'm getting the hang of this. I was using that % thing just to separate the right most number from the rest.

I started messing around with the program I said above, just done correctly.

```        Scanner in = new Scanner(System.in);

int input = in.nextInt();

int remainder = input % 10;

System.out.println("Input: " + input);
System.out.println("Remainder: " + remainder);```

It looks like that % operator doesn't actually affect the initial value of the number. Input stays the same, no matter what you put in there, and remainder is just the last digit of the number you enter.

Now I have another question. That homework I have pasted above said I need to encrypt a 4 digit number via that math process. I have the individual digits stored in an array. My question is, can I combine those numbers back into a single 4 digit int?  Reply With Quote

12. ## Re: Need some math help

what to write for that test program.
Yes something like that with 2-4 lines. No Scanner needed.
The print out should show you want the % operator does with 2 numbers. Don't always use % 10.
Change all the numbers, compile and execute several times to see what the results are with different values.  Reply With Quote

13. ## Re: Need some math help

so its just a way of separating the right most number from the left most?
Huh? No, it's the remainder resulting from the division of the left number by the right number. Changing my examples:

Division Result
5 / 10 = 0 remainder 5
12 / 10 = 1 remainder 2
27 / 10 = 2 remainder 7  Reply With Quote

14. ## Re: Need some math help

The divisor does not have to be 10:
3 % 5 = 3
5 % 3 = 2

Making a test program would allow you to try many combinations to see what % does.  Reply With Quote

15. ## Re: Need some math help

I could change the above program to have an int. A basic question for you all, is there a way to have output and input appear on the same line in the command prompt?

For example, I'd like to say something like "Enter the number to be precentified: " and then have the input be right next to it.  Reply With Quote

16. ## Re: Need some math help

Don't print a new line ("\n") at the end of the print out will leave the cursor on the same line.
Or use the print() method instead of the println() method.  Reply With Quote

17. ## Re: Need some math help

You guys have been a big help so far. Math has never been a friend to me.

Now two last questions I have.

Heres the code I have so far thanks to your help:
```	int input = Integer.parseInt(JOptionPane.showInputDialog("Please enter a 4 digit number"));

int[] numbers;

numbers = new int;

for (int i = 3; i > -1; i--)
{
numbers[i] = (input % 10);
input = input /10;
}

for (int i = 0; i < 4; i++)
{
int tempHolder;
tempHolder = numbers[i] + 7;
//System.out.println("Temp Holder: " + tempHolder);
numbers[i] = tempHolder % 10;
tempHolder = 0;
}

System.out.println();
System.out.println("The Encrypted numbers are: " + numbers + numbers + numbers + numbers);```

All I need now is a way to reverse what I've done. And it would be nice (but not required) to have the last number be a single int instead of an array, but thats not really a requirement, just something I'd like to do if at all possible.

Can my above operations be reversed?  Reply With Quote

18. ## Re: Need some math help

Can you make a simple list of the operations that the code is doing?  Reply With Quote

19. ## Re: Need some math help

sure

First a little message box pops up asking the user for a 4 digit number.

next I create a 4 member array.

After that I run that 4 digit number through a for loop, breaking off the numbers, and storing them in the array (last element first).

Then I run that array through another for loop, adding seven then doing the getting remainder through dividing by 10 (I have to do that in the instructions).

That last part is a little glossed over, but that's overall what I do. If you like I can go into a little more detail on that.

Finally I output the array in order from 0-3.  Reply With Quote

20. ## Re: Need some math help

last part is a little glossed over
Then it's not worth much when trying to design the code to write.

Which part are you trying to reverse?  Reply With Quote

21. ## Re: Need some math help

fair enough

that last part in slightly more detail.

it enters the final for loop. I declare a local integer to temporarily hold a value, and initialize it to the iteration of the loop plus 7. Then I assign the iteration of the loop to the temp value of the remainder after dividing by 10 (hope I said that right). Finally I reset the value of the local temp variable to zero.  Reply With Quote

22. ## Re: Need some math help

That's a paragraph, not a list of steps.
Something like this
declare a local integer
initialize it to the iteration of the loop plus 7
assign the iteration of the loop to the temp value
of the remainder after dividing by 10 ???? Not sure where this goes
reset the value of the local temp variable to zero.  Reply With Quote

23. ## Re: Need some math help

Thats actually the entire thing loop right there.

And I did the remainder after dividing by 10 thing because that's what the instructions said to do. Now I have to figure out how to reverse that. Technically it says to write another program to reverse it, but I'm 99% certain that he won't mind if I include the decryption in the same program.  Reply With Quote

24. ## Re: Need some math help

Can you correct my attempt at a list of the steps the code is doing now so we have something to work from towards reversing the steps.

Perhaps if you made an example it would help>
For example with the steps of 1) add 4 and 2) multiply by 7
x = 2
x = x+4 >> 6
x = x*7 >> 42

then the reverse would be
From above x = 42
x = x/7 >> 6
x = x - 4 >> 2  Reply With Quote

25. ## Re: Need some math help

not sure where your getting the 4 from. And what does >> mean? Is it like the % sign?

But I can try to make an example of my steps up to this point.

It may very well be that we simply can't reverse it, and I have to try from a different aproach.

First the user inputs some numbers. Lets say 1234

X=1234

Y (creates an array of way with 4 shelves/things to store numbers in)

Y = 1
Y = 2
Y = 3
Y = 4

Next I add 7 to the numbers

Y = 1+7
Y = 2+7
Y = 3+7
Y = 4+7

Then I do that get the remainder after dividing by 10 thing

Y = Y % 10
Y = Y % 10
Y = Y % 10
Y = Y % 10

Finally I output the numbers in an odd way the instructions tell me to, swapping the first and third spots, and the second and 4th spots

output Y Y Y Y

Edit: spelling  Reply With Quote

26. ## Re: Need some math help

The >> pointed to the results that would be in x. It was not meant as a java operator.

In your example what would be the values in the array at the end of each of the steps?
What math steps would need to be done to "undo" the % operator? Make a list of numbers that are possible values in this program (7-16), apply % to them and then look at what would need to be done to get back the original numbers.
For example:
7 % 10 = 7 Nothing needed to restore 7
16 % 10 = 6 add 10 to get 16  Reply With Quote