home / study / engineering / computer science / computer science questions and a
ID: 3737560 • Letter: H
Question
home / study / engineering / computer science / computer science questions and answers / need help. if multiplication is simply a form of iterative addition -- that 4 * 3 is shorthand ...
Question: Need help. If multiplication is simply a form of iterative addition -- that 4 * 3 is shorthand fo...
Need help.
If multiplication is simply a form of iterative addition -- that 4 * 3 is shorthand for 4 + 4 + 4 -- then what is division? Logically, if integer multiplication is iterative addition, then iteger division must be iterative subrtraction (with a remainder). Using the loop control structure, implement a subtration alternative to the division operation. The specifics are left to the programmer with the restriction that the answer must not employ Java's language-based implementation for either integer operator (ie do not use / or % when developing the arithmetic solution). A typical interation might look like this:
Enter two positive intergers for multiplication: 45 9
45 / 9 = 5 with a remainder of 0
Notes:
Develop your solution using Java.
Assume the first integer entered is the dividend and the second is the divisor i.e. from the example above, 45 is the dividend and 9 is the divisor.
Perform appropriate tests to insure user choice meets the criteria. While the Scanner method nextInt() will only accept integer values, test to insure the values entered are greater than zero (0). If the user enters an invalid value, identify the problem with an appropriate error message. Do notperform the arithmetic operation (and do not display a product). The program should display an error message and stop (not attempt to compute the product or prompt for a correct value)
Develop test data and record using an ASCII editor. Match your anticipated responses and the actual responses from the program. The test set should include correct and incorrect data.
After testing with your test data set, record the results as comments at the end of the EC01 source code file.
Submit the source program with the test data set, for credit.
Explanation / Answer
Program
import java.util.Scanner;
public class MyClass {
public static void main(String args[]) {
int num1, num2,i=0;
Scanner keyboard= new Scanner(System.in);
System.out.print("Type two numbers:");
num1= keyboard.nextInt();
num2= keyboard.nextInt();
while(num1>=num2){
num1=num1-num2;
i++;
}
System.out.println(" Quotient = " + i+" Remainder = "+num1);
}
}
Output
Type two numbers:10
3
Quotient = 3 Remainder = 1
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.