I have a question considering a problem I have worked on. Here is the problem statement:

My code Encryption part:Quote:

A company wants to transmit data over the telephone but is concerned that its phones may be tapped. It has asked you to write a program that will encrypt the data so that it may be transmitted more securely. All the data is transmitted as four-digit integers. Your application should read a four-digit integer entered by the user and encrypt it as follows: 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. Write a separate application that inputs an encrypted four-digit integer and decrypts it to form the original number.

Code :

package java4_37; import java.util.Scanner; public class Java4_37 { public static void main(String[] args) { int firstDigit, secondDigit, thirdDigit, fourthDigit, number, temp; Scanner input = new Scanner( System.in ); do{ System.out.print(" Enter Number: "); number = input.nextInt(); }while(number / 1000 == 0 || number / 10000 != 0 ); firstDigit = number / 1000; secondDigit = number / 100 % 10; thirdDigit = number / 10 % 10; fourthDigit = number % 10; firstDigit = (firstDigit + 7) % 10; secondDigit = (secondDigit + 7) % 10; thirdDigit = (thirdDigit + 7) % 10; fourthDigit = (fourthDigit + 7) % 10; temp = firstDigit; firstDigit = thirdDigit; thirdDigit = temp; temp = secondDigit; secondDigit = fourthDigit; fourthDigit = temp; System.out.printf("the encrypted number is %d%d%d%d\n", firstDigit, secondDigit, thirdDigit, fourthDigit); } }

and the decryption part:

Code :

package java4_37b; import java.util.Scanner; public class Java4_37b { public static void main(String[] args) { int firstDigit, secondDigit, thirdDigit, fourthDigit, number, temp; Scanner input = new Scanner( System.in ); do{ System.out.print(" Enter Number: "); number = input.nextInt(); }while(number / 1000 == 0 || number / 10000 != 0 ); firstDigit = number / 1000; secondDigit = number / 100 % 10; thirdDigit = number / 10 % 10; fourthDigit = number % 10; if(firstDigit <= 6 && firstDigit >= 0) firstDigit = firstDigit + 10; if(secondDigit <= 6 && secondDigit >= 0) secondDigit = secondDigit + 10; if(thirdDigit <= 6 && thirdDigit >= 0) thirdDigit = thirdDigit + 10; if(fourthDigit <= 6 && fourthDigit >= 0) fourthDigit = fourthDigit + 10; firstDigit = firstDigit - 7; secondDigit = secondDigit - 7; thirdDigit = thirdDigit - 7; fourthDigit = fourthDigit - 7; temp = firstDigit; firstDigit = thirdDigit * 1000; thirdDigit = temp * 10; temp = secondDigit; secondDigit = fourthDigit * 100; fourthDigit = temp; System.out.printf("the encrypted number is %d\n", firstDigit + secondDigit + thirdDigit + fourthDigit); } }

First, I tried that code myself and it was working fine (I don't know if it has any logical errors). Second thing and that's the reason I posted my question. One solution to this problem for decryption was to add 3 to the digit and then get the remainder of 10.

digit = (digit + 3) % 10;

and then swap digit to get the original number. But I don't know WHY we add 3. What is the logic of it. Actually, even if we converted it to an equation something like this:

(x + 7) % 10 = 4 where x represents the original digit before encryption

I don't remember that I used the remainder operator in any math classes :-? so I don't know who to get rid of it to solve the equation (we divide to get rid of multiplication and subtract to get rid of addition or vice versa).

So again what is the logic of adding 3 and using the remainder operator?