Here is my code without any for loops and conditions:
int decryptedCode[3] = {0};
int encryptedCode[3] = {0};
printf("Enter a four digit code. i.e (1234): ");
scanf("%d%d%d%d", &decryptedCode[0], &decryptedCode[1], &decryptedCode[2], &decryptedCode[3]);
encryptedCode[0] = (decryptedCode[0] + 7); //Encrypting Code
encryptedCode[1] = (decryptedCode[1] + 7); //Encrypting Code
encryptedCode[2] = (decryptedCode[2] + 7); //Encrypting Code
encryptedCode[3] = (decryptedCode[3] + 7); //Encrypting Code
if ( encryptedCode[0] > 9 ) encryptedCode[0] = encryptedCode[0]%10;
if ( encryptedCode[1] > 9 ) encryptedCode[1] = encryptedCode[1]%10;
if ( encryptedCode[2] > 9 ) encryptedCode[2] = encryptedCode[2]%10;
if ( encryptedCode[3] > 9 ) encryptedCode[3] = encryptedCode[3]%10;
encryptedCode[0] ^= encryptedCode[2]; //swap values
encryptedCode[2] ^= encryptedCode[0]; //swap values
encryptedCode[0] ^= encryptedCode[2]; //swap values
encryptedCode[1] ^= encryptedCode[3]; //swap values
encryptedCode[3] ^= encryptedCode[1]; //swap values
encryptedCode[1] ^= encryptedCode[3]; //swap values
printf("Encrypted four digit code: %d%d%d%d", encryptedCode[0], encryptedCode[1], encryptedCode[2], encryptedCode[3]);
here in the code below i added for loop n if-else condition to make code clean n efficient:
int counter;
int decryptedCode[3] = {0};
int encryptedCode[3] = {0};
printf("Enter a four digit code. i.e (1234): ");
scanf("%d%d%d%d", &decryptedCode[0], &decryptedCode[1], &decryptedCode[2], &decryptedCode[3]);
for ( counter = 0; counter<= 3; counter++ ) {
encryptedCode[counter] = (decryptedCode[counter] + 7);
if ( encryptedCode[counter] > 9 ) {
encryptedCode[counter] = encryptedCode[counter]%10;
}
}
encryptedCode[0] ^= encryptedCode[2]; //swap values
encryptedCode[2] ^= encryptedCode[0]; //swap values
encryptedCode[0] ^= encryptedCode[2]; //swap values
encryptedCode[1] ^= encryptedCode[3]; //swap values
encryptedCode[3] ^= encryptedCode[1]; //swap values
encryptedCode[1] ^= encryptedCode[3]; //swap values
printf("Encrypted four digit code: %d%d%d%d", encryptedCode[0], encryptedCode[1], encryptedCode[2], encryptedCode[3]);
but it is not working and i have no idea why, i tried to change many things all day long but no success.
int decryptedCode[4]andint encryptedCode[4].