Question

In: Computer Science

Playing with encryption: Write a program that will read a four-digit integer entered by the user...

Playing with encryption:

Write a program that will read a four-digit integer entered by the user and encrypt it as follows: Replace each digit with the result of adding 7 to the digit and getting the remainder after dividing the new value by 10. Then swap the second digit with the fourth. Finally, print the original number and its encrypted one. Now reverse the process. Read an encrypted integer and decrypt it by reversing the algorithm to obtain the original number. Print out both the encrypted and decrypted integer.

Solutions

Expert Solution

Programming Language is not Mentioned so I am doing it in c :

C Language Code:

#include<stdio.h>
int main(){
   int num,rem,option,dNum,oNum=0,n=1000,sec,four;
   //variables for storing the number,remainder , reading option and duplicate number dNum for storing num output number oNum
   printf("Enter 1.Encryption 2.Decryption");
   //reading which thing we do encryption or decryption
   scanf("%d",&option);
   printf("Enter four digit Number");
   scanf("%d",&num);
   dNum=num;
   if(option==1){
       while(dNum!=0){
           //getting the first number and add 7 to it and get remainter after that when divided by 10
           rem=dNum/n;
           rem=(rem+7)%10;
           oNum=oNum*10+rem;
           dNum=dNum%n;
          
           if(n==100){
               //storing second number
               sec=rem;
           }else if(n==1){
               //storing fourth number
               four=rem;
           }
           n=n/10;
       }
           printf("%d\t %d",sec,four);
       printf(" Number is %d\n",oNum);
       //making the second number zero and making fourth number there in that place
       oNum=oNum-(sec*100)+(four*100);
       //making the fourth number zero and making second number there in that place
       oNum=oNum-(four*1)+sec;
      
       printf("Original Number %d\n",num);
       printf("Encrypted Number is %d",oNum);
      
      
   }else{
           while(dNum!=0){
           //getting the first number and add 7 to it and get remainter after that when divided by 10
           rem=dNum/n;
           //reason for adding the +3 and substracting seven is explained in the pic
           if(rem>=7)
           rem=(rem-7)%10;
           else{
               rem=rem+3;
           }
           oNum=oNum*10+rem;
           dNum=dNum%n;
      
           if(n==100){
               //storing second number
               sec=rem;
           }else if(n==1){
               //storing fourth number
               four=rem;
           }
               n=n/10;
}
       //making the second number zero and making fourth number there in that place
       oNum=oNum-(sec*100)+(four*100);
       //making the fourth number zero and making second number there in that place
       oNum=oNum-(four*1)+sec;
      
       printf("Decrypted Number %d\n",num);
       printf("Original Number is %d",oNum);
      
   }
   return 0;
}

Reason for adding +3 and substracting -7 Explanation:

OUTPUT:

DECRYPTION OUTPUT:


Related Solutions

Write a C++ program that accepts a single integer value entered by user. If the value...
Write a C++ program that accepts a single integer value entered by user. If the value entered is less than one the program prints nothing. If the user enters a positive integer n. The program prints n x n box drawn with * characters. If the user enters 1 , for example the program prints *. If the user enter a 2, it prints ** ** that is , a 2x2 box of * symbols.
PYTHON Let n denote an integer entered by the user. Write a program to print n...
PYTHON Let n denote an integer entered by the user. Write a program to print n multiples of 5 in the descending order, with the last number being 5. Print the average of those n multiples
A. Write a program 1. Prompt the user to enter a positive integer n and read...
A. Write a program 1. Prompt the user to enter a positive integer n and read in the input. 2. Print out n of Z shape of size n X n side by side which made up of *. B. Write a C++ program that 1. Prompt user to enter an odd integer and read in the value to n. 2. Terminate the program if n is not odd. 3. Print out a cross shape of size n X n...
Write a C program that allows: Three integer values to be entered (read) from the keyboard,...
Write a C program that allows: Three integer values to be entered (read) from the keyboard, Display the sum of the three values on the computer screen as follows: The integers that you have entered are: a b c The sum of a , b & c is ______ Thank you! C Code: Output screen:
write a program that gets four digits from the user and tests to see which digit...
write a program that gets four digits from the user and tests to see which digit is the biggest and which is the smallest, use single-alternative only sample output is below: enter four digits and i will tell you which is biggest and smallest: 4 6 7 9 the biggest is 9 the smallest is 4
Write a program that uses a loop to read 10 integers from the user. Each integer...
Write a program that uses a loop to read 10 integers from the user. Each integer will be in the range from -100 to 100. After all 10 integers have been read, output the largest and smallest values that were entered, each on its own line in that order. Avoiding using the max or min functions from Python, and definitely use a loop to read the integers instead of 10 input statements.
Write a program that calculates mean and standard deviation for four user entered values. The most...
Write a program that calculates mean and standard deviation for four user entered values. The most common measures of a sample are the mean and the standard deviation. the mean is the sum of the values divided by the number of elements in the data set. The dispersion - or spread in the values - is measured by the standard deviation The equation for the mean is x¯ = x1 + x2 + · · · + xn The equation...
Write a program that asks the user for an integer. The program checks and prints to...
Write a program that asks the user for an integer. The program checks and prints to the screen whether the number is prime or not. For example, if user enters 17, the program should print “17 is prime”; if the user enters 20, the program should print “20 is not prime”. please do it with a “ while Loop”, Thanks..
Write a C++ program that finds the minimum number entered by the user .The user is...
Write a C++ program that finds the minimum number entered by the user .The user is allowed to enter negative numbers 0, or positive numbers. The program should be controlled with a loop statement. Break statements is not allowed to break from loop. After each number inputted by the user, The program will prompt user if they need to enter another number. User should enter either Y for yes or N for no. Make Sure the user enters either Y...
Create a program that will calculate the factorial of any user entered integer. For any positive integer, the factorial is given as
Create a program that will calculate the factorial of any user entered integer. For any positive integer, the factorial is given as: n! = n·(n-1)·(n-2)·.……·3·2·1. The factorial of 0 is 1. If a number is negative, factorial does not exist and this program should display error message. Sample output dialogues should look like these:  Enter an integer: -5 ERROR!!! Tactorial of a negative number doesn't exist  Enter an integer: 10  Factorial = 3628800
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT