Write an encryption and decryption program. Encrypt each digit by adding 7 and t
ID: 3537485 • Letter: W
Question
Write an encryption and decryption program. Encrypt each digit by adding 7 and taking the remainder after division by 10. After encrypting each digit, swap the first and third then swap the second and fourth. Decryption should reverse the process. Program must input a single integer and split it into 4 single digits (hint 1234%10 = 4 123%10 = 3 etc.). Program must do encode and decode in one file. Program must loop for continued inputs.
i think i have everything but the math out put is wrong on both ways from doing test on what was given (1234 = 0189) and visa versa the encryption gives me | 189| and decryption gives |20330| if you can show me were my math is wrong at and what i should do to fix it would be very helpfull.
#include <stdio.h>
#include<stdlib.h>
int main(void)
{
int encrypt;
int decrypt;
int e1, e2, e3, e4;
int d1, d2, d3, d4;
int a, x;
int o, d, t, q;
printf("Encode (1) Decode (2): " );
scanf("%d", &a);
if(a==1)
{
printf("Enter four digit number: ");
scanf("%d", &x);
q = x % 10;
x /= 10;
t = x % 10;
x /= 10;
d = x % 10;
x /= 10;
o = x % 10;
e1 = (o + 7) %10;
e2 = (d + 7) %10;
e3 = (t + 7) %10;
e4 = (q + 7) %10;
encrypt = (e3*1000) + (e4*100) + (e1*10) + e2;
printf("Encoded Digits: %4d ", encrypt);
}
else if (a == 2)
{
printf("Enter four digit number: ");
scanf("%d", &x);
q = x % 10;
x /= 10;
t = x % 10;
x /= 10;
d = x % 10;
x /= 10;
o = x % 10;
d1 = (o - 7)*10;
d2 = (d - 7)*10;
d3 = (t - 7)*10;
d4 = (q - 7)*10;
decrypt = (d4*1000) + (d3*100) + (d2*10) + d1;
printf("Encoded Digits: %4d ", decrypt);
}
else
{
printf("Encode (1) Decode (2): " );
scanf("%d", &a);
}
printf("Continue (1) Exit (0): ");
scanf("%d", &a);
if(a==1)
{
printf("Encode (1) Decode (2): " );
scanf("%d", &a);
if(a==1)
{
printf("Enter four digit number: ");
scanf("%d", &x);
q = x % 10;
x /= 10;
t = x % 10;
x /= 10;
d = x % 10;
x /= 10;
o = x % 10;
e1 = (o + 7) %10;
e2 = (d + 7) %10;
e3 = (t + 7) %10;
e4 = (q + 7) %10;
encrypt = (e3*1000) + (e4*100) + (e1*10) + e2;
printf("Encoded Digits: %4d ", encrypt);
}
else if(a == 2)
{
printf("Enter four digit number: ");
scanf("%d", &x);
q = x % 10;
x /= 10;
t = x % 10;
x /= 10;
d = x % 10;
x /= 10;
o = x % 10;
d1 = (o - 7)*10;
d2 = (d - 7)*10;
d3 = (t - 7)*10;
d4 = (q - 7)*10;
decrypt = (d4*1000) + (d3*100) + (d2*10) + d1;
printf("Encoded Digits: %4d ", decrypt);
}
else
{
printf("Encode (1) Decode (2): " );
scanf("%d", &a);
}
}
else if(a==0)
{
return(0);
}
else
{
printf("Continue (1) Exit (0): ");
scanf("%d", &a);
}
getch();
return(0);
}
Explanation / Answer
please rate - thanks
any question just ask, I will make any needed corrections
I changed it to a do...while loop
#include <stdio.h>
#include<stdlib.h>
int main(void)
{
int encrypt;
int decrypt;
int e1, e2, e3, e4;
int d1, d2, d3, d4;
int a, x;
int o, d, t, q;
do
{
printf("Encode (1) Decode (2): " );
scanf("%d", &a);
if(a==1)
{
printf("Enter four digit number: ");
scanf("%d", &x);
q = x % 10;
x /= 10;
t = x % 10;
x /= 10;
d = x % 10;
x /= 10;
o = x % 10;
e1 = (o + 7) %10;
e2 = (d + 7) %10;
e3 = (t + 7) %10;
e4 = (q + 7) %10;
encrypt = (e3*1000) + (e4*100) + (e1*10) + e2;
printf("Encoded Digits: %04d ", encrypt);
}
else if (a == 2)
{
printf("Enter four digit number: ");
scanf("%d", &x);
q = x % 10;
x /= 10;
t = x % 10;
x /= 10;
d = x % 10;
x /= 10;
o = x % 10;
d1 = (o +3)%10; //+3 = -7 +10 (in case -7 gives a negative
d2 = (d +3)%10;
d3 = (t +3)%10;
d4 = (q +3)%10;
decrypt = (d3*1000) + (d4*100) + (d1*10) + d2;
printf("Encoded Digits: %04d ", decrypt);
}
else
{
printf("Encode (1) Decode (2): " );
scanf("%d", &a);
}
printf("Continue (1) Exit (0): ");
scanf("%d", &a);
}while(a==1);
getch();
return(0);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.