Encryption


Joined
Nov 24, 2022
Messages
6
Reaction score
0
Hello everyone. I am learning C from a book and currently doing basic encryption assignment. The assignment calls to encrypt a four digit integer. To encrypt the integer the following steps must be taken: replace each digit with a result of adding 7 to the digit and getting a remainder after dividing new digit by 10. Then swap first digit with third and second digit with fourth. I am a little confused how I will be able to swap the digits. To solve the first part I would just break up the number into digit and do arithmetic manipulation. What should I do about swapping the digits? When I break up the number I guess I could store each digit in a separate variable where later I would be able to then swap them out. How would you handle it ? Just looking for suggestions. I should mention that so far in a course work I've only covered while loop, and if, else if statements and can only use what I have learned so far to solve this problem. Thanks for your time.
 
Ad

Advertisements

Joined
Nov 24, 2022
Messages
6
Reaction score
0
So far I am able to form a new number based on the requirement. Trying to implement the swap.
C:
int main(void)
{
    int number,remainder,diviser = 1000;

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

    while(remainder != 0) {
        number = remainder / diviser;
        remainder %= diviser;
        newNumber += ((number + 7) % 10) * diviser;
        diviser /= 10;
    }
    printf("%s%d","New formed number: ",newNumber);
    
     return 0;
}
 
Joined
Nov 24, 2022
Messages
6
Reaction score
0
Hi everyone. I was able to find a solution for this and solve it as such. I am not sure if it was the right way to do it but it works. The only formatting I didn't implement is if the new number is encrypted to 0000, there is no swapping numbers as they are same. Please let me know if I should have handled it differently somehow. Thanks.


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

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

    while(remainder != 0) {
        number = remainder / diviser;
        remainder %= diviser;
        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;
    if (remainder < 1000) {
        if(remainder < 100){
            printf("%s%d","Encrypted number is : 00",remainder);
        }else {
            printf("\n%s%d","Encrypted number: 0",remainder);
        }
    }else {
        printf("\n%s%d","Encrypted number: ",remainder);
    }
     return 0;
}
 
Joined
Nov 24, 2022
Messages
6
Reaction score
0
I thought to update this, since someone showed me that printf can format numbers therefore eliminating the rest of the code.

Code:
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;
}
 
Joined
Sep 21, 2022
Messages
19
Reaction score
3
I thought to update this, since someone showed me that printf can format numbers therefore eliminating the rest of the code.

Code:
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;
}

There are 2 divisions in the loop, it can be done without division.

What happens when 1000 is entered? Will newNumber be 8777?

4 digits to process, so the the program should always loop 4 times.
 
Joined
Nov 24, 2022
Messages
6
Reaction score
0
There are 2 divisions in the loop, it can be done without division.

What happens when 1000 is entered? Will newNumber be 8777?

4 digits to process, so the the program should always loop 4 times.
You are right, it breaks down. :-(, I am going to have to do more work on it. Thanks.
 
Ad

Advertisements

Joined
Nov 24, 2022
Messages
6
Reaction score
0
There are 2 divisions in the loop, it can be done without division.

What happens when 1000 is entered? Will newNumber be 8777?

4 digits to process, so the the program should always loop 4 times.
It was fixed, thanks for pointing it out.

Code:
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(diviser != 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;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top