Question
Description: Write a C program that performs simple encryption or decryption on a four digit integer. The program should prompt the user to enter the
Description: Write a C program that performs simple encryption or decryption on a four digit integer. The program should prompt the user to enter the one letter command "e" (without quotes) for encryption, "d" for decryption or "t" for terminating the program. If the user wants to encrypt or decrypt, the program should give a prompt asking for a four digit integer, perform the operations, display the result, and repeat the initial prompt.
Use the following steps for encryption and decryption:
Encryption:
separate the integer into its 4 digits
take the first digit, add 7 to it and then perform a mod 10 operation
repeat the same process as above for the other digits
swap the first and the third digits
swap the second and the fourth digits
assemble the digits to get the encrypted number
display the encrypted number
Decryption: For decryption follow the above process, except that you should add 3 to each digit and then perform a mod 10 operation.
See the given outline for the programs interaction with the user.
Sample Algorithm
Things to remember for the second programming assignment. declare the function prototypes for functions "encrypt", "decrypt", and "clearbuffer". // Define function "main" int main( ) { declare and initialize variables use the variable 'done' to control the loop initialize 'done' to zero ( meaning false) while( not done ) prompt the user to enter 'e', 'd', or 't' read the input clearbuffer( ); if( input equals 't' ) we are done, this means setting 'done' to 1 (true) else if( input equals 'e' ) prompt the user to input a four digit integer read the integer clearbuffer( ); call the function "encrypt" to encrypt the integer display the result else if( input equals 'd' ) prompt the user to input a four digit integer read the integer clearbuffer( ); call the function "decrypt" to decrypt the integer display the result else prompt the user that the input was wrong end while print termination message return EXIT_SUCCESS signal to the operating system } // Function "encrypt" takes one parameter of type integer and returns an // integer representing the encrypted number. int encrypt(int n) { declare and initialize variables if necessary separate the the integer n into four digits perform the required operations on each digit swap first and third digits swap second and fourth digits assemble the encrypted number return it as the value of the function } // Function "decrypt" has similar logic and structure, except that you // should add 3 (instead of 7) before performing the "mod 10" operation. // Function "clearbuffer" // Note that this function is a "void" function and is defined as such. void clearbuffer( ) { int ch; while ( (ch = getchar( )) != EOF && ch != ' ' ) ; // this clears the input buffer }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started