/*HEllo everybody. i would like to see your recommendations and your quotes.
how can i optimize this code?
what is wrong into the code?
what can it be improved?
this is the problem
(Cryptography) A company wants to transmit information across the telephone, but it worries their telephones could be controlled. All the information is transmitted as points of four digits. The company has asked you to write a program that codes its information, so that these could be transmitted by more safet. Your program must read a number of four digits introduced by the user and to code it of the following way: replace every digit (the result of adding 7 to the digit) module 10. Then exchange the first digit with the third , and exchange the second digit with the quarter.
Print the ciphered point.
*/
/*this is my solution*/
///Criptografía, ejercicio 4.34... Codificar
///Luis Fernando Pinzon.
///14/08/2018
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main(int argc, char** argv)
{
//variables
int digitOne = 0;
int digitTwo = 0;
int digitThree = 0;
int digitFour = 0;
int digitAmount = 7;
int amountOne = 0;
int amountTwo = 0;
int amountThree = 0;
int amountFour = 0;
//positions or locations
int locationOne = 0;
int locationTwo = 0;
int locationThree = 0;
int locationFour = 0;
//Access identifier
int number;
//to enter
cout << "Enter a number to encode: ";
cin >> number;
cout << "\n";
if(number - 1000 >= 0) //to validate only four digits MINIMUM
{
if(number - 10000 < 0)// to validate only four digits MAXIMUM
{
//to split each digit of the number
digitOne = number / 1000;
digitTwo = ((number / 100)%10);
digitThree = ((number / 10) % 10);
digitFour = number % 10 ;
//to store the amount
amountOne = digitOne + digitAmount;
amountTwo = digitTwo + digitAmount;
amountThree = digitThree + digitAmount;
amountFour = digitFour + digitAmount;
//to store the operation between the amount plus residue split 10
digitOne = (amountOne * (amountOne%10));
digitTwo = (amountTwo * (amountTwo%10));
digitThree = (amountThree * (amountThree%10));
digitFour = (amountFour * (amountFour%10));
//to be located in their respective positions
locationOne = digitThree;
locationTwo = digitFour;
locationThree = digitOne;
locationFour = digitTwo;
cout << "the encoded number is: " << locationOne << "" << locationTwo << "" << locationThree << "" << locationFour;
}
else{
cout << "Introduce a number with integer digits only 4. ";
}
}
else
{
cout << "Introduce a number with integer digits only 4. ";
}
return 0;
}
#include <iostream>
int main()
{
std::cout << "enter a number to encode (exactly four digits): " ;
int number ;
if( std::cin >> number ) // if the user entered a number
{
if( number > 999 && number < 10'000 ) // if it has exactly four digits
{
// extract the four digits
int digit_4 = number%10 ;
int digit_3 = (number/10) % 10 ;
int digit_2 = (number/100) % 10 ;
int digit_1 = (number/1'000) % 10 ;
// replace every digit with the result of adding 7 to the digit module 10.
constint addend = 7 ;
digit_4 = (digit_4+addend) % 10 ;
digit_3 = (digit_3+addend) % 10 ;
digit_2 = (digit_2+addend) % 10 ;
digit_1 = (digit_1+addend) % 10 ;
// the encoded number is formed by exchanging the first digit with the third,
// and the second digit with the fourth.
std::cout << "the encoded number is: " << digit_3 << digit_4 << digit_1 << digit_2 << '\n' ;
}
else std::cout << "error: not a four digit number\n" ;
}
else std::cout << "error: non-numeric input\n" ;
}