# CBasic encryption.

#### Crutoy37

##### New Coder
Hi everyone. I am self-studying C from a book C How to program and came upon assignment that requires you to encrypt a four-digit integer. The requirements are as follows: Replace each digit with result of adding a 7 to the digit and getting a remainder by dividing by 10. Then swap first digit with third, and fourth digit with second. I am not sure yet how I will change the digits places but I think I can solve the first part. I would break down the number in each separate digit and do the arithmetic manipulation and then would have to multiply each number into appropriate power to bring it back as a whole. To swap the numbers, do I just break the number up and store each digit in a separate variable ? The course only covered so far while loop, if, else if statements. Just looking for suggestions on how to solve the swapping digits part. Thanks for your time.

#### cbreemer

##### King Coder
Yes, you first of all break up the number into 4 separate variables. Or better, into an array of 4 integers so you can loop through them. When you're done changing and swapping them, reassemble the array into a single 4-digit integer.

#### Crutoy37

##### New Coder
I am not up to the arrays yet. I was able to solve it as such. Thanks for responding.

C:
``````int main(void)
{
int number = 0,newNumber = 0,remainder,diviser = 1000;

printf("%s","Enter a four digit number: ");
scanf("%d",&remainder);

//breaking up the numbers into left most digit
while(remainder != 0) {
number = remainder / diviser;
remainder %= diviser;
/*
forming a new number by adding 7 to each digit and
getting its remainder, then forming a new
number by multiplying to appropriate
power with each pass
*/
newNumber += ((number + 7) % 10) * diviser;
diviser /= 10;
}
printf("%s%d\n","New formed number: ",newNumber);
//swap of the digits
remainder = newNumber % 100;
number = newNumber / 100;
remainder = remainder * 100 + number;
printf("\n%s%04d","Encrypted number: ",remainder);

return 0;
}``````

#### cbreemer

##### King Coder
Yes that works, well done. Indeed you don't need an array to form the new number.
Interesting idea to swap the digits by arithmetic ! Technically this is not really swapping (although in this case the effect is the same) so I'm not sure if your solution would be approved by a tutor. Had the swapping requirement been different (like just swap digit 1 with digit 3), I don't think you could have pulled such a trick 😀

#### Crutoy37

##### New Coder
Yes that works, well done. Indeed you don't need an array to form the new number.
Interesting idea to swap the digits by arithmetic ! Technically this is not really swapping (although in this case the effect is the same) so I'm not sure if your solution would be approved by a tutor. Had the swapping requirement been different (like just swap digit 1 with digit 3), I don't think you could have pulled such a trick 😀
I had to fix one line in the code, replaced the expression in the while loop with
Code:
``  while(diviser != 0)``

#### cbreemer

##### King Coder
I had to fix one line in the code, replaced the expression in the while loop with
Code:
``  while(diviser != 0)``
Ah right, I had not spotted that 😳
Curiously, your code seemed to work fine for me nonetheless. Maybe I just got lucky.

#### Crutoy37

##### New Coder
Ah right, I had not spotted that 😳
Curiously, your code seemed to work fine for me nonetheless. Maybe I just got lucky.
It did work but if the number was for instance 1000 it would break.

Replies
5
Views
386
Replies
2
Views
235
Replies
1
Views
660
Replies
4
Views
244
Replies
2
Views
282