# Multiply two polynomials

Here given code implementation process.

// C Program
// Multiply two polynomials
#include <stdio.h>
// Print polynomial
void printPolynomial(int polynomial[], int n)
{
for (int i = 0; i < n; ++i)
{
if (i != 0)
{
printf(" + %dx^%d", polynomial[i], i);
}
else
{
printf("%d", polynomial[i]);
}
}
printf("\n");
}

void multiplyPolynomials(int x[], int y[], int a, int b)
{
// Display polynomial
printPolynomial(x, a);
printPolynomial(y, b);
int n = a + b - 1;
// Use to collect result element
int z[n];
// Set initial value
for (int i = 0; i < n; ++i)
{
z[i] = 0;
}
for (int i = 0; i < a; ++i)
{
for (int j = 0; j < b; ++j)
{
z[i + j] += (x[i] *y[j]);
}
}
// Display calculated result
printPolynomial(z, n);
}
int main()
{
// Given polynomials
int x[] = {
7 , 8 , 6 , 1 , 3
};
int y[] = {
5 , 1 , 3 , 2
};
// Get the size
int a = sizeof(x) / sizeof(x[0]);
int b = sizeof(y) / sizeof(y[0]);
// Test
multiplyPolynomials(x, y, a, b);
return 0;
}

#### Output

7 + 8x^1 + 6x^2 + 1x^3 + 3x^4
5 + 1x^1 + 3x^2 + 2x^3
35 + 47x^1 + 59x^2 + 49x^3 + 50x^4 + 18x^5 + 11x^6 + 6x^7
/*
Java program
Multiply two polynomials
*/
public class Multiplication
{
// Print polynomial
public void printPolynomial(int[] polynomial, int n)
{
for (int i = 0; i < n; ++i)
{
if (i != 0)
{
System.out.print(" + " + polynomial[i] + "x^" + i);
}
else
{
System.out.print(polynomial[i]);
}
}
System.out.print("\n");
}
public void multiplyPolynomials(int[] x, int[] y, int a, int b)
{
// Display polynomial
printPolynomial(x, a);
printPolynomial(y, b);
int n = a + b - 1;
// Use to collect result element
int[] z = new int[n];
// Set initial value
for (int i = 0; i < n; ++i)
{
z[i] = 0;
}
for (int i = 0; i < a; ++i)
{
for (int j = 0; j < b; ++j)
{
z[i + j] += (x[i] * y[j]);
}
}
// Display calculated result
printPolynomial(z, n);
}
public static void main(String[] args)
{
Multiplication task = new Multiplication();
// Given polynomials
int[] x = {
7 , 8 , 6 , 1 , 3
};
int[] y = {
5 , 1 , 3 , 2
};
// Get the size
int a = x.length;
int b = y.length;
// Test
task.multiplyPolynomials(x, y, a, b);
}
}

#### Output

7 + 8x^1 + 6x^2 + 1x^3 + 3x^4
5 + 1x^1 + 3x^2 + 2x^3
35 + 47x^1 + 59x^2 + 49x^3 + 50x^4 + 18x^5 + 11x^6 + 6x^7
// Include header file
#include <iostream>
using namespace std;
/*
C++ program
Multiply two polynomials
*/
class Multiplication
{
public:
// Print polynomial
void printPolynomial(int polynomial[], int n)
{
for (int i = 0; i < n; ++i)
{
if (i != 0)
{
cout << " + " << polynomial[i] << "x^" << i;
}
else
{
cout << polynomial[i];
}
}
cout << "\n";
}
void multiplyPolynomials(int x[], int y[], int a, int b)
{
// Display polynomial
this->printPolynomial(x, a);
this->printPolynomial(y, b);
int n = a + b - 1;
// Use to collect result element
int z[n];
// Set initial value
for (int i = 0; i < n; ++i)
{
z[i] = 0;
}
for (int i = 0; i < a; ++i)
{
for (int j = 0; j < b; ++j)
{
z[i + j] += (x[i] *y[j]);
}
}
// Display calculated result
this->printPolynomial(z, n);
}
};
int main()
{
Multiplication *task = new Multiplication();
// Given polynomials
int x[] = {
7 , 8 , 6 , 1 , 3
};
int y[] = {
5 , 1 , 3 , 2
};
// Get the size
int a = sizeof(x) / sizeof(x[0]);
int b = sizeof(y) / sizeof(y[0]);
// Test
task->multiplyPolynomials(x, y, a, b);
return 0;
}

#### Output

7 + 8x^1 + 6x^2 + 1x^3 + 3x^4
5 + 1x^1 + 3x^2 + 2x^3
35 + 47x^1 + 59x^2 + 49x^3 + 50x^4 + 18x^5 + 11x^6 + 6x^7
// Include namespace system
using System;
/*
Csharp program
Multiply two polynomials
*/
public class Multiplication
{
// Print polynomial
public void printPolynomial(int[] polynomial, int n)
{
for (int i = 0; i < n; ++i)
{
if (i != 0)
{
Console.Write(" + " + polynomial[i] + "x^" + i);
}
else
{
Console.Write(polynomial[i]);
}
}
Console.Write("\n");
}
public void multiplyPolynomials(int[] x, int[] y, int a, int b)
{
// Display polynomial
this.printPolynomial(x, a);
this.printPolynomial(y, b);
int n = a + b - 1;
// Use to collect result element
int[] z = new int[n];
// Set initial value
for (int i = 0; i < n; ++i)
{
z[i] = 0;
}
for (int i = 0; i < a; ++i)
{
for (int j = 0; j < b; ++j)
{
z[i + j] += (x[i] * y[j]);
}
}
// Display calculated result
this.printPolynomial(z, n);
}
public static void Main(String[] args)
{
Multiplication task = new Multiplication();
// Given polynomials
int[] x = {
7 , 8 , 6 , 1 , 3
};
int[] y = {
5 , 1 , 3 , 2
};
// Get the size
int a = x.Length;
int b = y.Length;
// Test
task.multiplyPolynomials(x, y, a, b);
}
}

#### Output

7 + 8x^1 + 6x^2 + 1x^3 + 3x^4
5 + 1x^1 + 3x^2 + 2x^3
35 + 47x^1 + 59x^2 + 49x^3 + 50x^4 + 18x^5 + 11x^6 + 6x^7
package main
import "fmt"
/*
Go program
Multiply two polynomials
*/

// Print polynomial
func printPolynomial(polynomial[] int, n int) {
for i := 0 ; i < n ; i++ {
if i != 0 {
fmt.Print(" + ", polynomial[i], "x^", i)
} else {
fmt.Print(polynomial[i])
}
}
fmt.Print("\n")
}
func multiplyPolynomials(x[] int, y[] int, a int, b int) {
// Display polynomial
printPolynomial(x, a)
printPolynomial(y, b)
var n int = a + b - 1
// Use to collect result element
var z = make([] int, n)
// Set initial value
for i := 0 ; i < n ; i++ {
z[i] = 0
}
for i := 0 ; i < a ; i++ {
for j := 0 ; j < b ; j++ {
z[i + j] += (x[i] * y[j])
}
}
// Display calculated result
printPolynomial(z, n)
}
func main() {

// Given polynomials
var x = [] int {7 , 8 , 6 , 1 , 3}
var y = [] int {5 , 1 , 3 , 2}
// Get the size
var a int = len(x)
var b int = len(y)
// Test
multiplyPolynomials(x, y, a, b)
}

#### Output

7 + 8x^1 + 6x^2 + 1x^3 + 3x^4
5 + 1x^1 + 3x^2 + 2x^3
35 + 47x^1 + 59x^2 + 49x^3 + 50x^4 + 18x^5 + 11x^6 + 6x^7
<?php
/*
Php program
Multiply two polynomials
*/
class Multiplication
{
// Print polynomial
public	function printPolynomial(\$polynomial, \$n)
{
for (\$i = 0; \$i < \$n; ++\$i)
{
if (\$i != 0)
{
echo(" + ".\$polynomial[\$i].
"x^".\$i);
}
else
{
echo(\$polynomial[\$i]);
}
}
echo("\n");
}
public	function multiplyPolynomials(\$x, \$y, \$a, \$b)
{
// Display polynomial
\$this->printPolynomial(\$x, \$a);
\$this->printPolynomial(\$y, \$b);
\$n = \$a + \$b - 1;
// Use to collect result element
\$z = array_fill(0, \$n, 0);
// Set initial value
for (\$i = 0; \$i < \$n; ++\$i)
{
\$z[\$i] = 0;
}
for (\$i = 0; \$i < \$a; ++\$i)
{
for (\$j = 0; \$j < \$b; ++\$j)
{
\$z[\$i + \$j] += (\$x[\$i] * \$y[\$j]);
}
}
// Display calculated result
\$this->printPolynomial(\$z, \$n);
}
}

function main()
{
\$task = new Multiplication();
// Given polynomials
\$x = array(7, 8, 6, 1, 3);
\$y = array(5, 1, 3, 2);
// Get the size
\$a = count(\$x);
\$b = count(\$y);
// Test
\$task->multiplyPolynomials(\$x, \$y, \$a, \$b);
}
main();

#### Output

7 + 8x^1 + 6x^2 + 1x^3 + 3x^4
5 + 1x^1 + 3x^2 + 2x^3
35 + 47x^1 + 59x^2 + 49x^3 + 50x^4 + 18x^5 + 11x^6 + 6x^7
/*
Node JS program
Multiply two polynomials
*/
class Multiplication
{
// Print polynomial
printPolynomial(polynomial, n)
{
for (var i = 0; i < n; ++i)
{
if (i != 0)
{
process.stdout.write(" + " +
polynomial[i] + "x^" + i);
}
else
{
process.stdout.write("" +
polynomial[i]);
}
}
process.stdout.write("\n");
}
multiplyPolynomials(x, y, a, b)
{
// Display polynomial
this.printPolynomial(x, a);
this.printPolynomial(y, b);
var n = a + b - 1;
// Use to collect result element
var z = Array(n).fill(0);
// Set initial value
for (var i = 0; i < n; ++i)
{
z[i] = 0;
}
for (var i = 0; i < a; ++i)
{
for (var j = 0; j < b; ++j)
{
z[i + j] += (x[i] * y[j]);
}
}
// Display calculated result
this.printPolynomial(z, n);
}
}

function main()
{
var task = new Multiplication();
// Given polynomials
var x = [7, 8, 6, 1, 3];
var y = [5, 1, 3, 2];
// Get the size
var a = x.length;
var b = y.length;
// Test
task.multiplyPolynomials(x, y, a, b);
}
main();

#### Output

7 + 8x^1 + 6x^2 + 1x^3 + 3x^4
5 + 1x^1 + 3x^2 + 2x^3
35 + 47x^1 + 59x^2 + 49x^3 + 50x^4 + 18x^5 + 11x^6 + 6x^7
#    Python 3 program
#    Multiply two polynomials
class Multiplication :
#  Print polynomial
def printPolynomial(self, polynomial, n) :
i = 0
while (i < n) :
if (i != 0) :
print(" + ", polynomial[i] ,"x^", i, end = "",sep="")
else :
print(polynomial[i], end = "")

i += 1

print(end = "\n")

def multiplyPolynomials(self, x, y, a, b) :
#  Display polynomial
self.printPolynomial(x, a)
self.printPolynomial(y, b)
n = a + b - 1
#  Use to collect result element
z = [0] * (n)
i = 0
#  Set initial value
while (i < n) :
z[i] = 0
i += 1

i = 0
while (i < a) :
j = 0
while (j < b) :
z[i + j] += (x[i] * y[j])
j += 1

i += 1

#  Display calculated result
self.printPolynomial(z, n)

def main() :
#  Given polynomials
x = [7, 8, 6, 1, 3]
y = [5, 1, 3, 2]
#  Get the size
a = len(x)
b = len(y)
#  Test
task.multiplyPolynomials(x, y, a, b)

if __name__ == "__main__": main()

#### Output

7 + 8x^1 + 6x^2 + 1x^3 + 3x^4
5 + 1x^1 + 3x^2 + 2x^3
35 + 47x^1 + 59x^2 + 49x^3 + 50x^4 + 18x^5 + 11x^6 + 6x^7
#    Ruby program
#    Multiply two polynomials
class Multiplication
#  Print polynomial
def printPolynomial(polynomial, n)
i = 0
while (i < n)
if (i != 0)
print(" + ", polynomial[i] ,"x^", i)
else

print(polynomial[i])
end

i += 1
end

print("\n")
end

def multiplyPolynomials(x, y, a, b)
#  Display polynomial
self.printPolynomial(x, a)
self.printPolynomial(y, b)
n = a + b - 1
#  Use to collect result element
z = Array.new(n) {0}
i = 0
#  Set initial value
while (i < n)
z[i] = 0
i += 1
end

i = 0
while (i < a)
j = 0
while (j < b)
z[i + j] += (x[i] * y[j])
j += 1
end

i += 1
end

#  Display calculated result
self.printPolynomial(z, n)
end

end

def main()
#  Given polynomials
x = [7, 8, 6, 1, 3]
y = [5, 1, 3, 2]
#  Get the size
a = x.length
b = y.length
#  Test
task.multiplyPolynomials(x, y, a, b)
end

main()

#### Output

7 + 8x^1 + 6x^2 + 1x^3 + 3x^4
5 + 1x^1 + 3x^2 + 2x^3
35 + 47x^1 + 59x^2 + 49x^3 + 50x^4 + 18x^5 + 11x^6 + 6x^7
/*
Scala program
Multiply two polynomials
*/
class Multiplication()
{
// Print polynomial
def printPolynomial(polynomial: Array[Int], n: Int): Unit = {
var i: Int = 0;
while (i < n)
{
if (i != 0)
{
print(" + " + polynomial(i) + "x^" + i);
}
else
{
print(polynomial(i));
}
i += 1;
}
print("\n");
}
def multiplyPolynomials(x: Array[Int], y: Array[Int],
a: Int, b: Int): Unit = {
// Display polynomial
printPolynomial(x, a);
printPolynomial(y, b);
var n: Int = a + b - 1;
// Use to collect result element
var z: Array[Int] = Array.fill[Int](n)(0);
var i: Int = 0;
// Set initial value
while (i < n)
{
z(i) = 0;
i += 1;
}
i = 0;
while (i < a)
{
var j: Int = 0;
while (j < b)
{
z(i + j) += (x(i) * y(j));
j += 1;
}
i += 1;
}
// Display calculated result
printPolynomial(z, n);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Multiplication = new Multiplication();
// Given polynomials
var x: Array[Int] = Array(7, 8, 6, 1, 3);
var y: Array[Int] = Array(5, 1, 3, 2);
// Get the size
var a: Int = x.length;
var b: Int = y.length;
// Test
task.multiplyPolynomials(x, y, a, b);
}
}

#### Output

7 + 8x^1 + 6x^2 + 1x^3 + 3x^4
5 + 1x^1 + 3x^2 + 2x^3
35 + 47x^1 + 59x^2 + 49x^3 + 50x^4 + 18x^5 + 11x^6 + 6x^7
import Foundation;
/*
Swift 4 program
Multiply two polynomials
*/
class Multiplication
{
// Print polynomial
func printPolynomial(_ polynomial: [Int], _ n: Int)
{
var i: Int = 0;
while (i < n)
{
if (i  != 0)
{
print(" + ", polynomial[i] ,"x^",
i,separator :"", terminator: "");
}
else
{
print(polynomial[i], terminator: "");
}
i += 1;
}
print(terminator: "\n");
}
func multiplyPolynomials(_ x: [Int], _ y: [Int], _ a: Int, _ b: Int)
{
// Display polynomial
self.printPolynomial(x, a);
self.printPolynomial(y, b);
let n: Int = a + b - 1;
// Use to collect result element
var z: [Int] = Array(repeating: 0, count: n);
var i: Int = 0;
// Set initial value
while (i < n)
{
z[i] = 0;
i += 1;
}
i = 0;
while (i < a)
{
var j: Int = 0;
while (j < b)
{
z[i + j] += (x[i] * y[j]);
j += 1;
}
i += 1;
}
// Display calculated result
self.printPolynomial(z, n);
}
}
func main()
{
let task: Multiplication = Multiplication();
// Given polynomials
let x: [Int] = [7, 8, 6, 1, 3];
let y: [Int] = [5, 1, 3, 2];
// Get the size
let a: Int = x.count;
let b: Int = y.count;
// Test
task.multiplyPolynomials(x, y, a, b);
}
main();

#### Output

7 + 8x^1 + 6x^2 + 1x^3 + 3x^4
5 + 1x^1 + 3x^2 + 2x^3
35 + 47x^1 + 59x^2 + 49x^3 + 50x^4 + 18x^5 + 11x^6 + 6x^7
/*
Kotlin program
Multiply two polynomials
*/
class Multiplication
{
// Print polynomial
fun printPolynomial(polynomial: Array < Int > , n: Int): Unit
{
var i: Int = 0;
while (i < n)
{
if (i != 0)
{
print(" + " + polynomial[i] + "x^" + i);
}
else
{
print(polynomial[i]);
}
i += 1;
}
print("\n");
}
fun multiplyPolynomials(x: Array < Int > ,
y: Array < Int > , a: Int, b: Int): Unit
{
// Display polynomial
this.printPolynomial(x, a);
this.printPolynomial(y, b);
val n: Int = a + b - 1;
// Use to collect result element
val z: Array < Int > = Array(n)
{
0
};
var i: Int = 0;
// Set initial value
while (i < n)
{
z[i] = 0;
i += 1;
}
i = 0;
while (i < a)
{
var j: Int = 0;
while (j < b)
{
z[i + j] += (x[i] * y[j]);
j += 1;
}
i += 1;
}
// Display calculated result
this.printPolynomial(z, n);
}
}
fun main(args: Array < String > ): Unit
{
val task: Multiplication = Multiplication();
// Given polynomials
val x: Array < Int > = arrayOf(7, 8, 6, 1, 3);
val y: Array < Int > = arrayOf(5, 1, 3, 2);
// Get the size
val a: Int = x.count();
val b: Int = y.count();
// Test
task.multiplyPolynomials(x, y, a, b);
}

#### Output

7 + 8x^1 + 6x^2 + 1x^3 + 3x^4
5 + 1x^1 + 3x^2 + 2x^3
35 + 47x^1 + 59x^2 + 49x^3 + 50x^4 + 18x^5 + 11x^6 + 6x^7

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.