Reputation: 232
I've been working on this problem for the past two hours or so and I've encountered a roadblock. I can actually perform the division but when it comes time to print the remainder that isn't 0 the output doesn't match. I would like to know what is it what I'm doing wrong.
public class Division {
public static void main(String[] args) {
int numerator = 0;
int numeratorprint = 0;
int denominator = 0;
int product = 0;
int remainder = 0;
int counter = 1;
Scanner input;
input = new Scanner(System.in);
System.out.print("Enter two positive intergers for division:\n");
numerator = input.nextInt();
numeratorprint = numerator;
denominator = input.nextInt();
while ((numerator < 0) || (denominator < 0)) {
System.out.print("Enter two positive intergers for division:\n");
numerator = input.nextInt();
denominator = input.nextInt();
}
if (numerator == 0){
System.out.print(numerator + "/" + denominator + " = " + numerator + " with a remainder of " + numerator);
}
else if (denominator == 0){
System.out.print("This result is undefined (Cannot divide by Zero)");
}
else if (denominator > numerator){
System.out.print("Cannot do proper fractions");
}
else {
while (numerator > denominator){
counter++;
numerator = numerator - denominator;
}
}
product = counter * denominator;
remainder = numeratorprint - product;
System.out.println(numeratorprint + " / " + denominator + " = " + counter + " with a remainder of " + remainder);
}
}
/* Sample I/O 1
Enter two positive intergers for division:
25
5
OUTPUT:
25 / 5 = 5 with a remainder of 0
Sample I/O 2
Enter two positive intergers for division:
27
5
OUTPUT:
27 / 5 = 6 with a remainder of -3
*/
Upvotes: 2
Views: 1817
Reputation: 2738
I saw few errors in your code. 1. Redundant variable 2 why don`t you use modular operator ? 3. you can remove a while loop too
public class Division {
public static void main(String[] args) {
Scanner input;
input = new Scanner(System.in);
System.out.print("Enter two positive intergers for division:\n");
int numerator = input.nextInt();
int numeratorprint = numerator;
int denominator = input.nextInt();
int quotient = 0;
int remainder = 0;
while ((numerator < 0) || (denominator < 0)) {
System.out.print("Enter two positive intergers for division:\n");
numerator = input.nextInt();
denominator = input.nextInt();
}
if (numerator == 0) {
System.out.print(numerator + "/" + denominator + " = " + numerator + " with a remainder of " + numerator);
} else {
if (denominator == 0) {
System.out.print("This result is undefined (Cannot divide by Zero)");
} else {
if (denominator > numerator) {
System.out.print("Cannot do proper fractions");
} else {
remainder = numerator % denominator;
quotient = ((numerator / denominator) - (remainder / denominator));
}
}
}
System.out.println(numeratorprint + " / " + denominator + " = " + quotient + " with a remainder of " + remainder);
}
}
Upvotes: 0
Reputation: 100
I believe you have an off by one error where you are starting your counter at 1 rather than 0. I tested a few numbers and changing this seems to fix it just fine. So change
int counter = 1;
to
int counter = 0;
Also, you must change your while loop conditional on the else block to be
while (numerator >= denominator) {...
in order to account for the case of when the divisor evenly divides the dividend.
Upvotes: 1