Question: This program will be run interactively using Python; the user should be prompted to enter an integer and then your program will produce another integer
This program will be run interactively using Python; the user should be prompted to enter an integer and then your program will produce another integer depending on whether the input was even or odd.
The following is an example of what you might see when you run the program; the input you type is shown in green (press Enter at the end of a line), and the output generated by the program is shown in black text.
Enter an integer: 123 Number is odd, doubling each digit in the integer... Result: 112233 Would you like to enter another integer (y/n): y Enter an integer: 456 Number is even, tripling each digit in the integer... Result: 444555666 Would you like to enter another integer (y/n): y Enter an integer: 124 Number is even, tripling each digit in the integer... Result: 111222444 Would you like to enter another integer (y/n): y Enter an integer: 123 Number is odd, doubling each digit in the integer... Result: 112233 Would you like to enter another integer (y/n): n
When the entered number is odd, you double each digit in the integer, below are a few examples:
Value Result 1 11 13 1133 1357 11335577
When the entered number is even, you triple each digit in the integer, below are a few examples:
Value Result 00 12 111222 124 111222444
You may assume that all of the integers entered (odd or even) will be >= 0.
CS1064 Spring 2017
Also notice that your program should ask whether another integer should be entered. If the response to the prompt is "y" your program should ask for another integer and perform the appropriate computations, if the user inputs an "n" the program should end. At this point you don't have to worry about incorrect input, when asked "Would you like to enter another integer (y/n): ", the input will always be a "y" or an "n" character.
Your program should be able to handle an arbitrary number of integers. In the example above there were only 4 integers processed, but you should be able to handle 10, 20, 50, or more different integers during the course of running your program without issue.
The Mod (%) Operator
The mod operator produces the remainder of an integer division, so 0 % 2 = 0, 1 % 2 = 1, 2 % 2 = 0, and 3 % 2 = 1. One common use of the mod operator is determining whether a number is even or odd. If you mod with 2 and the result is 0, the number is even; if the result is 1, the number is odd:
1234 % 2 = 0 # Zero remainder, even. 123 % 2 = 1 # Non-zero remainder, not even.
Further, let's say I want to get each of the digits in the integer in 1234, I can do that with the mod operator and integer division:
# Get the least significant digit. 1234 % 10 = 4 # Shave off one digit using division. 1234 // 10 = 123
# Get the next least significant digit. 123 %10=3 # Shave off one digit using division. 123 // 10 = 12
# Get the next least significant digit. 12 % 10 = 2 # Shave off one digit using division. 12 // 10 = 1
# Get the next least significant digit. 1 % 10 = 1 # Shave off one digit using division. 1 // 10 = 0
Your program should able to handle an integer with any number of digits, so you should generalize this example with some sort of loop.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
