Programming following the instructions below Take the time t
Programming following the instructions below. Take the time to write out an algorithm for these problems to help clarify the logic before starting programming. Remember to also include comments in each step to display a general explanation of what your program does. Make sure your program be displayed same as the last three images.
Solution
#include <iostream>
#include <string>
using namespace std;
int digits(int);
int main() {
int n,p1,p2,p3,p4,tmp,enc;
cout << \"Welcome to Nancy\'s 4 digit encryptor\ \";
cout << \"Please enter a 4 digit number you wish to encrypt:\";
cin >> n;
if(digits(n)!=4){ // check number of digits equals 4
cout << \"Sorry, but this is not a 4 digit number. Program will terminate.\";
}
else{
// convert each digit to increment by 7 modulo 10
p4 = ((n%10)+7)%10;
n = n/10;
p3 = ((n%10)+7)%10;
n = n/10;
p2 = ((n%10)+7)%10;
n = n/10;
p1 = ((n%10)+7)%10;
// swap 1st to 3rd and 2nd to 4th positions.
tmp = p1;
p1 = p3;
p3 = tmp;
tmp = p2;
p2 = p4;
p4 = tmp;
// create number
enc = p4 + (p3*10) + (p2*100) + (p1*1000);
cout << \"The encrypted version of your input is \"<< enc << \"\ \";
}
cout << \"Thank you for using Nancy\'s 4 digit encryptor program.\ \";
}
int digits(int n){
int d = 0;
while(n>0){
n = n/10;
d++;
}
return d;
}
/*
sample output
*/
