I am making a program that does synthetic division, except for it is not showing the quotient only the remainder. I have checked my for loops so many times, and I can't understant what is happening here is my code:
import java.util.*;
import java.lang.*;
class synthetic {
static Scanner userInput = new Scanner(System.in);
public static void main(String[] args)
{
System.out.print("What is the highest degree of the polynomial function: ");
int degree = userInput.nextInt();
synthetic(degree);
}
public static void synthetic(int degree)
{
int[] coefficients = new int[degree+1];
for(int i=0; i<=degree; i++)
{
if(i == 0)
{
System.out.print("Leading coefficient: ");
coefficients[i] = userInput.nextInt();
}
else if(i == degree)
{
System.out.print("Last coefficient: ");
coefficients[i] = userInput.nextInt();
}
else
{
System.out.print("Next coefficient: ");
coefficients[i] = userInput.nextInt();
}
}
System.out.println(Arrays.toString(coefficients));
System.out.print("What is the divisor: ");
int divisor = userInput.nextInt();
System.out.println(divisor);
divide(coefficients, divisor);
}
public static void divide(int[] coefficients, int divisor)
{
int a = coefficients[0];
int[] quotient = new int[coefficients.length-1];
for(int i = 1; i<coefficients.length; i++)
{
int b = a*divisor;
a = coefficients[i] + b;
quotient[i-1] = a;
}
System.out.println(Arrays.toString(quotient)); for(int i=1; i<=quotient.length; i++)
{
if(i == quotient.length)
{
if(divisor<0) System.out.print(quotient[quotient.length-1]+"/x+"+Math.abs(divisor));
else System.out.print(quotient[quotient.length-1]+"/x-"+Math.abs(divisor));
}
else
{
int x = 0;
for(int q = quotient.length-1; q<0; q--)
{
if(q == 1)
{
System.out.print(quotient[quotient.length-2]+"x + ");
}
else
{
System.out.print(quotient[x]+"x^"+q+" + ");
}
}
}
}
}
}