Hello everyone. I am self learning C from the Dietel and Dietel book C How to program. I am doing an assignment at the end of the chapter that requires a user to enter a four digit integer that will be encrypted 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 first digit with third, and second with fourth. Then display the digit. I think I can solve the first part of the problem. I am not sure how I will switch the digits places. Any suggestions is appreciated. So far the material have only covered while loop, if, else if, and I can only use what's available to me up to this point. I look forward to your suggestions. Thank you.
For further actions, you may consider blocking this person and/or reporting abuse
Top comments (5)
To calculate the result of the swap you'll do:
third*1000 + fourth*100 + first*10 + second.
To get the individual digits you can use division and modulo arithmetic.
For example given the starting number 1234, to get the second digit:
(1234 mod 1000) รท 100. (This will give 2.)
You might need to cast to int to get rid of the fractional part.
So what I did so far was form a new number based on the requirement.
`
int main(void)
{
int number, newNumber,remainder,diviser = 1000;
}
`
I'll work now on implementing the swap.
Thanks I'll give it a try.
I've just had someone show me that printf function can format number with specifications. So it totally eliminates the rest of the code. Pretty cool.
`int main(void)
{
int number = 0,newNumber = 0,remainder,diviser = 1000;
}
`
Hi everyone. So this is how I solve the above problem. I am not sure If I took the shortcuts to come up with this. If I did please let me know. Program works as is, the only formatting I didn't do is the new encrypted digit is evaluated to 0000, there is no order to swap anything. I left it as is.
`
int main(void)
{
int number,newNumber = 0,remainder,diviser = 1000;
}
`