221k views
3 votes
Objectives: Use if statements and loops An ancient method for multiplying two integers works by repeated multiplication and division by 2. Let's say you have two numbers (X and Y). Multiply X by 2 and divide (integer division) Y by 2. Repeat the process until Y becomes zero and you can't divide any further. The result of multiplying X by Y is the total of all the values of X whenever y was odd. Here is an example of multiplying 30 by 18: X Y 18 9 4 2 1 Result = 60 + 480 = 540. Write a C++ program that repeatedly asks the user (y/n question) for two positive integers to multiply and uses the method outlined above to calculate and display the result of multiplication. Validate the input values to make sure they are both positive. Your program should repeatedly ask the user to reenter each number until they enter a positive integer. 30 60 120 240 480 Result = 60+ 480 = 540. Write a C++ program that repeatedly asks the user (y/n question) for two positive integers to multiply and uses the method outlined above to calculate and display the result of multiplication. Validate the input values to make sure they are both positive. Your program should repeatedly ask the user to reenter each number until they enter a positive integer. Sample Interaction: Enter a positive number: 10 Enter a positive number: 5 The product is 50 Continue (y/n)? y Keep rejecting negative values until a positive value is entered Enter a positive number: -10 Enter a positive number: -5 Enter a positive number: 10 Enter a positive number: -4 Enter a positive number: -4 Enter a positive number: 4 The product is 40 Continue (y/n)? y Enter a positive number: 0 Enter a positive number: 9 The product is 0 Continue (y/n)? n Hints: • Use the mod (%) operator to find if a number is odd Use a loop to read an integer. Keep looping as long as the user enters a negative value You only need to worry about integer values Grading: Programs that contain syntax errors will earn zero points. Programs that use global variables other than constants will earn zero points. Programs that use any library that was not discussed in class will earn zero points. Your grade will be determine using the following criteria: Correctness (25 points) o. The program runs continuously until the user chooses to quit. o All output results are displayed as requested and have accurate values. o All instructions are followed. o Clarity and format of the output • Style & Documentation (5 points).

User Joanq
by
7.5k points

1 Answer

7 votes

Final answer:

To write a C++ program that calculates and displays the result of multiplication using the ancient method described, you should use if statements and loops. Validate the input values to make sure they are both positive. Use a while loop to multiply X by 2 and divide Y by 2 until Y becomes zero. Display the result of the multiplication.

Step-by-step explanation:

To write a C++ program that calculates and displays the result of multiplication using the ancient method described, you should use if statements and loops. First, prompt the user to enter two positive integers. Validate the input values to make sure they are both positive. Use a loop to repeatedly ask the user to reenter each number until they enter a positive integer. Then, use a while loop to multiply X by 2 and divide Y by 2 until Y becomes zero. Keep track of the sum of X whenever Y was odd. Finally, display the result of the multiplication.

User Draksia
by
8.6k points

No related questions found