In: Computer Science
Create a Java method that does the following: 1) Ask the user for a dividend and a divisor both of "int" type. 2) Computes the remainder of the division. The quotient (answer) must be of the "int" type. Do NOT use the method " % " provided in Java in your code. Remember that it gives wrong answers when some of the inputs are negative. Please see the videos for the explanation. This is the code I have from the previous work that should be modified to fit this problem:
import java.util.Scanner;
public class DivisionAlgorithm {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n1, n2;
System.out.print("Enter dividend input: ");
n1 = scanner.nextInt();
System.out.print("Enter divisor input: ");
n2 = scanner.nextInt();
while(n1>=n2){
n1 = n1 - n2;
}
System.out.println("Quotient = "+n1);
}
}
-22 / 3 doesn't compute correctly, negative numbers return with errors.
import java.util.Scanner;
public class DivisionAlgorithm {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n1, n2;
System.out.print("Enter dividend input: ");
n1 = scanner.nextInt();
System.out.print("Enter divisor input: ");
n2 = scanner.nextInt();
if(n1>0)
{
while(n1>=n2){
n1 = n1 - n2;
}
}
else
{
while(n1<n2)
{
n1=n1+n2;
}
n1=n1-n2;
}
System.out.println("Quotient = "+n1);
}
}