I have an assignment to be done in college, a piece asks for the instructions below to be done. Please don’t give away the answer, simply leading me in the right direction would be awesome.
All the data is transmitted as four-digit integers. You program 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 the second with the fourth.
i can do everything but swapping the digits at the end of the instructions.
All help greatly appreciated,
thanks guys!
First of all, you need to get every digit in the number and store in separate variables (in an array or a list).
Let’s say
Your array would look like this:
Then for each member of the list, do this:
list[i] = (list[i] + 7) % 10;
Then swap the elements in the list as directed. Write a swap function so that you can reuse it.
Then construct your number out of the array. Note that array indexes will be the power of 10 when summing up the figures.
This solution will easily scale to n-digit integers.