A friend asks you to send him your social security number so that he can use it
ID: 3569976 • Letter: A
Question
A friend asks you to send him your social security number so that he can use it to open up a bank account in both your names. You do not want to send it by email, as you are scared of identity theft, so you decide to encrypt the number. You choose a very simple encryption scheme.
a. Place the social security number in a array with 9 elements
b. Subtract a specified number (input by a user and <5) from each of the
array elements.
c. If an element becomes negative add 10 to it.
d. Then shift all the numbers to the left by an amount equal to the number
input by the user
Therefore if your social security number is 987654321 and the user inputs 2, the program should output 765432109 after the first subtraction and for the rotation it should output as the final encrypted number 097654321
Explanation / Answer
#include<iostream>
#include<stdio.h>
using namespace std;
void printArr(int arr[], int num){
for(int i=0; i<num; i++){
printf("%d ",arr[i]);
}
printf(" ");
}
void swap(int *a, int *b){
*a = *a + * b;
*b = *a - *b;
*a = *a - *b;
}
void reverseArr(int arr[], int start, int end){
while(start < end){
swap(&arr[start], &arr[end]);
start++;
end--;
}
}
void rotateArr(int arr[], int k, int num){
reverseArr(arr, 0, k-1);
reverseArr(arr, k, num-1);
reverseArr(arr, 0, num-1);
}
int main(){
int arr[10] = {0};
int num , sub;
cout << "Enter Social Security number : " ;
cin >> num;
for(int i=8; i>= 0; i--){
arr[i] = num%10;
num = num/10;
}
cout << "Number to subtract from each number : ";
cin >> sub;
for(int i=0; i<9; i++){
arr[i] = arr[i] - sub;
if(arr[i] < 0){
arr[i] += 10;
}
}
rotateArr(arr, 9-sub, 9);
printArr(arr, 9);
return 0;
}