# Untitled

unknown
plain_text
7 months ago
2.4 kB
2
Indexable
Never
```package com.bocodes.integer.division;

public class IntegerDivisionMaker {

public static void makeDivision(int dividend, int divisor) {
validateInput(dividend, divisor);
longDivision(dividend, divisor);

String divisionSteps = longDivision(dividend, divisor);
System.out.println("Long Division Method:");
System.out.println(divisionSteps);
}

public static String longDivision(int dividend, int divisor) {
int quotient = dividend / divisor;
int remainder = dividend % divisor;
int[] digits = digitMaker(dividend);

StringBuilder sb = new StringBuilder();
sb.append(dividend).append(" | ").append(divisor).append("\n");
sb.append("Quotient").append(quotient).append("Remainder").append(remainder);

int currentDividend = remainder;
int index = digits.length - 1;

while (index >= 0) {
currentDividend = currentDividend * 10 + digits[index];
int currentQuotient = currentDividend / divisor;
int currentRemainder = currentDividend % divisor;

sb.append(", ").append(currentQuotient);
currentDividend = currentRemainder;
index--;
}
sb.append(", ...");
sb.append("\n\nFinal Result:\n");
sb.append("Quotient: ").append(quotient).append(", Remainder: ").append(remainder);

return sb.toString();
}

public static int[] digitMaker(int dividend) {

int dividendLength = (int) Math.log10(dividend) + 1;
int[] digits = new int[dividendLength];
int digitIndex = dividendLength - 1;

while (dividend > 0) {
int digit = dividend % 10;
digits[digitIndex] = digit;
dividend /= 10;
digitIndex--;
}

return digits;
}

private static void validateInput(Integer dividend, Integer divisor) {
if (dividend == null) {
throw new IllegalArgumentException("can not accept null");
}
if (divisor == null) {
throw new IllegalArgumentException("can not accept null");
}

if (dividend == 0 || divisor == 0) {
throw new IllegalArgumentException("Can not divide by zero");

}
}
}
```