Question

In: Computer Science

Write a program to multiply two polynomials. Code needed in Java.

Write a program to multiply two polynomials.

Code needed in Java.

Solutions

Expert Solution

Java Code

import java.util.Scanner;
class Main
{
static int[] mul(int C[], int D[],
int m, int n)
{
int[] p= new int[m + n - 1];
  
for (int k = 0; k < m + n - 1; k++)
{
p[k] = 0;
}
  
for (int i = 0; i < m; i++) // Multiply two polynomials term by term
{
for (int j = 0; j < n; j++)
{
p[i + j] += C[i] * D[j]; // saving the result after Multiply
}
}
  
return p;
}
  
static void print(int pi[], int k) // function to print the polynomial
{
for (int j = 0; j < k; j++)
{
System.out.print(pi[j]);
if (j != 0)
{
System.out.print("x^" + j);
}
if (j != k - 1)
{
System.out.print(" + ");
}
}
}
  

public static void main(String[] args)
{
  
int p,i,j;
Scanner s = new Scanner(System.in);
System.out.print("\nEnter the highest degree of the First polynomial :");
p = s.nextInt(); //input the highest degree of the First polynomial
int A[] = new int[p+1];
for(i = 0; i<=p; i++)
{ System.out.println("Enter the value for x^"+i);
  
A[i] = s.nextInt();
  
}
  
// The polynomial represents in the format 3 + 2x + 5x^2

System.out.print("\n\nEnter the highest degree of the Second polynomial :");
p = s.nextInt(); //input the highest degree of the second polynomial
int B[] = new int[p+1];
for(i = 0; i<=p; i++)
{ System.out.println("Enter the value for x^"+i);
  
B[i] = s.nextInt();
  
}
int f = A.length; //finding the size
int k = B.length;
  
System.out.println("\n\nFirst polynomial is ");
print(A, f); //function call
System.out.println("\n Second polynomial is ");
print(B, k); //function call
  
int[] l= mul(A,B,f,k);
//function call to multiply
System.out.println("\n\n Product polynomial =");
print(l, f + k - 1);
  
  
}
}

Output

(4+8x) * (5+2x) = 20+48x+16x2

(5+10x2+6x3 ) * ( 1+2x+4x2 )


Related Solutions

Write a program to multiply a polynomial with a given number. Code needed in java.
Write a program to multiply a polynomial with a given number. Code needed in java.
Write a program to multiply a polynomial with a given number. Code needed in java.
Write a program to multiply a polynomial with a given number. Code needed in java.
Problem 1. We are going to multiply the two polynomials A(x) = 5 − 3x and...
Problem 1. We are going to multiply the two polynomials A(x) = 5 − 3x and B(x) = 4 + 2x to produce C(x) = a + bx + cx2 in three different ways. Do this by hand, and show your work. (a) Multiply A(x) × B(x) algebraically. (b) (i) Evaluate A and B at the three (real) roots of unity 1, i, −1. (Note that we could use any three values.) (ii) Multiply the values at the three roots...
Write a Python program to add, multiply and divide any two numbers.
Write a Python program to add, multiply and divide any two numbers.
Write a program that adds and subtracts two polynomials. It creates an array of nodes and...
Write a program that adds and subtracts two polynomials. It creates an array of nodes and connects them into the freeStore. This implementation uses one array to store multiple array to store multiple polynomial instances and the free store. I need help to finish the LinkedListInArrayPolynomial class. Output should look like below: Forth test is linked list of terms in an array. linkInArray1 = 3x^11+4x^10+4x^4 linkInArray2 = 4x^19+5x^14-3x^12-78 sum of linkInArray1 and linkInArray2 = 4x^19+5x^14-3x^12+3x^11+4x^10+4x^4-78 linkInArray1 minus linkInArray2 = -4x^19-5x^14+3x^12+3x^11+4x^10+4x^4+78...
P-3.36 Write a Java program for a matrix class that can add and multiply arbitrary twodimensional...
P-3.36 Write a Java program for a matrix class that can add and multiply arbitrary twodimensional arrays of integers.
Write an arm assembly program that will multiply two arrays (index by index) and store the...
Write an arm assembly program that will multiply two arrays (index by index) and store the result in a third array. Declare an array: .data Arr1: .quad 10    #index 0             .quad 4      #index 1          ….. Arr2: .quad 2,5,6…. Arr3: .quad 0,0,0…. To load array pointer address:      Movq $Arr1, %rdx   #stores address of Arr1 index 0 in rdx To move to the next index of an array:     Addq $8,%rdx To retrieve data: Movq (%rdx), %rcx         # will...
Write a C++ program to multiply two matrices a and b and print the result. Use...
Write a C++ program to multiply two matrices a and b and print the result. Use two-dimensional arrays to represent the matrices.
Java Script Ask the user for a Fahrenheit temperature Write the code needed to convert a...
Java Script Ask the user for a Fahrenheit temperature Write the code needed to convert a Fahrenheit temperature into a Celsius temperature. Use an alert box to show the result Ask the user for a Celsius temperature Write a function to convert from Celsius to Fahrenheit; use an alert box to show the results Decide on two other conversions (meters to feet, pounds to kilos, other) Ask the user for numbers to be converted; be sure to tell them what...
Program in Java code Write a program with total change amount in pennies as an integer...
Program in Java code Write a program with total change amount in pennies as an integer input, and output the change using the fewest coins, one coin type per line. The coin types are Dollars, Quarters, Dimes, Nickels, and Pennies. Use singular and plural coin names as appropriate, like 1 Penny vs. 2 Pennies. .Ex1: If the input is: 0 the output is:    No change            Ex2: If the input is:    45   the output is:   1 Quarter 2 Dimes
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT