Answer to Question #149161 in C++ for mohammed

Question #149161
A company wants to transmit data over the telephone, but it is concerned that its phones may be tapped. All of its data is transmitted as four-digit integers. It has asked you to write a program that will encrypt its data so that the data may be transmitted more securely. Your program should read a four-digit integer entered by the user in a prompt dialog and encrypt it as follow: Replace each digit by (the sum of that digit plus 7) modulus 10. Then swap the first digit with the third, and swap the second digit with the fourth. Then display the encrypted integer.

I.2 (10 points)
Write a program to decrypt the encrypted data.
1
Expert's answer
2020-12-06T10:23:57-0500
#include <iostream>
#include <stdlib.h>
#include <cmath>
using namespace std;


int main () {
    
    // the telephone dialogs transmitted as four digit numbers;
    int four_digit_number;
    cout << "Enter four digit number: ";
    cin >> four_digit_number;
    
    int digit_1, digit_2, digit_3, digit_4;
    
    // we find every digit number
    digit_1 = four_digit_number / 1000;
    digit_2 = four_digit_number / 100 - 10 * digit_1;
    digit_3 = four_digit_number / 10 - (100 * digit_1 + 10 * digit_1);
    digit_4 = four_digit_number % 10;
    
    // we declare an array for placing digits;
    int arr[4] = {digit_1, digit_2, digit_3, digit_4};
    
    // this cycle for Replacing each digit by (the sum of that digit plus 7) modulus 10.
    // we declare an array for encrypted digits;
    int arrEncrypted[4];
    
    for (int  i = 0; i < 4; i++) {
        arrEncrypted[i] = (arr[i] + 7) % 10;
    }
    
    // this process for swaping the first digit with the third, and swaping the second digit with the fourth
    // we declare an integer for swap;
    
    int temp;
    temp = arrEncrypted[0];
    arrEncrypted[0] = arrEncrypted[2];
    arrEncrypted[2] = temp;
    
    temp = arrEncrypted[1];
    arrEncrypted[1] = arrEncrypted[3];
    arrEncrypted[3] = temp;
    
    int encrypted_number = 0;
    for (int i = 0; i < 4; i++) {
        encrypted_number += pow(10, 3 - i) *  arrEncrypted[i];
    }
    
    cout << "The encrypted four digit number is : " << encrypted_number;
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog