Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

C Basic encryption.

Crutoy37

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.
 
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.
 
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;
}
 
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 😀
 
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)
 
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.
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom