Add of two polynomials using array

Here given code implementation process.

// C Program
// Add of two polynomials using array
#include <stdio.h>

// Display polynomial sequence
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");
}
int maxLength(int a, int b)
{
	if (a > b)
	{
		return a;
	}
	return b;
}
void addPolynomials(int x[], int y[], int a, int b)
{
	// Display polynomial
	printPolynomial(x, a);
	printPolynomial(y, b);
	// Get max length
	int n = maxLength(a, b);
	// Use to collect result element
	int z[n];
	for (int i = 0; i < n; ++i)
	{
		if (i < a && i < b)
		{
			// Case ➀ : When have both polynomials element exists
			z[i] = x[i] + y[i];
		}
		else if (i < a)
		{
			// Case ➁ : When have x polynomial element exists
			z[i] = x[i];
		}
		else
		{
			// Case ➂ : When have y polynomial element exists
			z[i] = y[i];
		}
	}
	// 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
	addPolynomials(x, y, a, b);
	return 0;
}

Output

7 + 8x^1 + 6x^2 + 1x^3 + 3x^4
5 + 1x^1 + 3x^2 + 2x^3
12 + 9x^1 + 9x^2 + 3x^3 + 3x^4
/*
    Java program
    Add of two polynomials using array
*/
public class Addition
{
	// Display polynomial sequence
	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 int maxLength(int a, int b)
	{
		if (a > b)
		{
			return a;
		}
		return b;
	}
	public void addPolynomials(int[] x, int[] y, int a, int b)
	{
		// Display polynomial
		printPolynomial(x, a);
		printPolynomial(y, b);
		// Get max length
		int n = maxLength(a, b);
		// Use to collect result element
		int[] z = new int[n];
		for (int i = 0; i < n; ++i)
		{
			if (i < a && i < b)
			{
				// Case ➀ : When have both polynomials element exists
				z[i] = x[i] + y[i];
			}
			else if (i < a)
			{
				// Case ➁ : When have x polynomial element exists
				z[i] = x[i];
			}
			else
			{
				// Case ➂ : When have y polynomial element exists
				z[i] = y[i];
			}
		}
		// Display calculated result
		printPolynomial(z, n);
	}
	public static void main(String[] args)
	{
		Addition task = new Addition();
		// 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.addPolynomials(x, y, a, b);
	}
}

Output

7 + 8x^1 + 6x^2 + 1x^3 + 3x^4
5 + 1x^1 + 3x^2 + 2x^3
12 + 9x^1 + 9x^2 + 3x^3 + 3x^4
// Include header file
#include <iostream>
using namespace std;
/*
    C++ program
    Add of two polynomials using array
*/
class Addition
{
	public:
		// Display polynomial sequence
		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";
		}
	int maxLength(int a, int b)
	{
		if (a > b)
		{
			return a;
		}
		return b;
	}
	void addPolynomials(int x[], int y[], int a, int b)
	{
		// Display polynomial
		this->printPolynomial(x, a);
		this->printPolynomial(y, b);
		// Get max length
		int n = this->maxLength(a, b);
		// Use to collect result element
		int z[n];
		for (int i = 0; i < n; ++i)
		{
			if (i < a && i < b)
			{
				// Case ➀ : When have both polynomials element exists
				z[i] = x[i] + y[i];
			}
			else if (i < a)
			{
				// Case ➁ : When have x polynomial element exists
				z[i] = x[i];
			}
			else
			{
				// Case ➂ : When have y polynomial element exists
				z[i] = y[i];
			}
		}
		// Display calculated result
		this->printPolynomial(z, n);
	}
};
int main()
{
	Addition *task = new Addition();
	// 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->addPolynomials(x, y, a, b);
	return 0;
}

Output

7 + 8x^1 + 6x^2 + 1x^3 + 3x^4
5 + 1x^1 + 3x^2 + 2x^3
12 + 9x^1 + 9x^2 + 3x^3 + 3x^4
// Include namespace system
using System;
/*
    Csharp program
    Add of two polynomials using array
*/
public class Addition
{
	// Display polynomial sequence
	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 int maxLength(int a, int b)
	{
		if (a > b)
		{
			return a;
		}
		return b;
	}
	public void addPolynomials(int[] x, int[] y, int a, int b)
	{
		// Display polynomial
		this.printPolynomial(x, a);
		this.printPolynomial(y, b);
		// Get max length
		int n = this.maxLength(a, b);
		// Use to collect result element
		int[] z = new int[n];
		for (int i = 0; i < n; ++i)
		{
			if (i < a && i < b)
			{
				// Case ➀ : When have both polynomials element exists
				z[i] = x[i] + y[i];
			}
			else if (i < a)
			{
				// Case ➁ : When have x polynomial element exists
				z[i] = x[i];
			}
			else
			{
				// Case ➂ : When have y polynomial element exists
				z[i] = y[i];
			}
		}
		// Display calculated result
		this.printPolynomial(z, n);
	}
	public static void Main(String[] args)
	{
		Addition task = new Addition();
		// 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.addPolynomials(x, y, a, b);
	}
}

Output

7 + 8x^1 + 6x^2 + 1x^3 + 3x^4
5 + 1x^1 + 3x^2 + 2x^3
12 + 9x^1 + 9x^2 + 3x^3 + 3x^4
package main
import "fmt"
/*
    Go program
    Add of two polynomials using array
*/
type Addition struct {}
func getAddition() * Addition {
	var me *Addition = &Addition {}
	return me
}
// Display polynomial sequence
func(this Addition) 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(this Addition) maxLength(a, b int) int {
	if a > b {
		return a
	}
	return b
}
func(this Addition) addPolynomials(x[] int, 
						y[] int, a int, b int) {
	// Display polynomial
	this.printPolynomial(x, a)
	this.printPolynomial(y, b)
	// Get max length
	var n int = this.maxLength(a, b)
	// Use to collect result element
	var z = make([] int, n)
	for i := 0 ; i < n ; i++ {
		if i < a && i < b {
			// Case ➀ : When have both polynomials element exists
			z[i] = x[i] + y[i]
		} else if i < a {
			// Case ➁ : When have x polynomial element exists
			z[i] = x[i]
		} else {
			// Case ➂ : When have y polynomial element exists
			z[i] = y[i]
		}
	}
	// Display calculated result
	this.printPolynomial(z, n)
}
func main() {
	var task * Addition = getAddition()
	// 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
	task.addPolynomials(x, y, a, b)
}

Output

7 + 8x^1 + 6x^2 + 1x^3 + 3x^4
5 + 1x^1 + 3x^2 + 2x^3
12 + 9x^1 + 9x^2 + 3x^3 + 3x^4
<?php
/*
    Php program
    Add of two polynomials using array
*/
class Addition
{
	// Display polynomial sequence
	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 maxLength($a, $b)
	{
		if ($a > $b)
		{
			return $a;
		}
		return $b;
	}
	public	function addPolynomials($x, $y, $a, $b)
	{
		// Display polynomial
		$this->printPolynomial($x, $a);
		$this->printPolynomial($y, $b);
		// Get max length
		$n = $this->maxLength($a, $b);
		// Use to collect result element
		$z = array_fill(0, $n, 0);
		for ($i = 0; $i < $n; ++$i)
		{
			if ($i < $a && $i < $b)
			{
				// Case ➀ : When have both polynomials element exists
				$z[$i] = $x[$i] + $y[$i];
			}
			else if ($i < $a)
			{
				// Case ➁ : When have x polynomial element exists
				$z[$i] = $x[$i];
			}
			else
			{
				// Case ➂ : When have y polynomial element exists
				$z[$i] = $y[$i];
			}
		}
		// Display calculated result
		$this->printPolynomial($z, $n);
	}
}

function main()
{
	$task = new Addition();
	// 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->addPolynomials($x, $y, $a, $b);
}
main();

Output

7 + 8x^1 + 6x^2 + 1x^3 + 3x^4
5 + 1x^1 + 3x^2 + 2x^3
12 + 9x^1 + 9x^2 + 3x^3 + 3x^4
/*
    Node JS program
    Add of two polynomials using array
*/
class Addition
{
	// Display polynomial sequence
	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");
	}
	maxLength(a, b)
	{
		if (a > b)
		{
			return a;
		}
		return b;
	}
	addPolynomials(x, y, a, b)
	{
		// Display polynomial
		this.printPolynomial(x, a);
		this.printPolynomial(y, b);
		// Get max length
		var n = this.maxLength(a, b);
		// Use to collect result element
		var z = Array(n).fill(0);
		for (var i = 0; i < n; ++i)
		{
			if (i < a && i < b)
			{
				// Case ➀ : When have both polynomials element exists
				z[i] = x[i] + y[i];
			}
			else if (i < a)
			{
				// Case ➁ : When have x polynomial element exists
				z[i] = x[i];
			}
			else
			{
				// Case ➂ : When have y polynomial element exists
				z[i] = y[i];
			}
		}
		// Display calculated result
		this.printPolynomial(z, n);
	}
}

function main()
{
	var task = new Addition();
	// 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.addPolynomials(x, y, a, b);
}
main();

Output

7 + 8x^1 + 6x^2 + 1x^3 + 3x^4
5 + 1x^1 + 3x^2 + 2x^3
12 + 9x^1 + 9x^2 + 3x^3 + 3x^4
#    Python 3 program
#    Add of two polynomials using array
class Addition :
	#  Display polynomial sequence
	def printPolynomial(self, polynomial, n) :
		i = 0
		while (i < n) :
			if (i != 0) :
				print(" +", polynomial[i] ,"x^", i, end = "")
			else :
				print(polynomial[i], end = "")
			
			i += 1
		
		print(end = "\n")
	
	def maxLength(self, a, b) :
		if (a > b) :
			return a
		
		return b
	
	def addPolynomials(self, x, y, a, b) :
		#  Display polynomial
		self.printPolynomial(x, a)
		self.printPolynomial(y, b)
		#  Get max length
		n = self.maxLength(a, b)
		#  Use to collect result element
		z = [0] * (n)
		i = 0
		while (i < n) :
			if (i < a and i < b) :
				#  Case ➀ : When have both polynomials element exists
				z[i] = x[i] + y[i]
			elif (i < a) :
				#  Case ➁ : When have x polynomial element exists
				z[i] = x[i]
			else :
				#  Case ➂ : When have y polynomial element exists
				z[i] = y[i]
			
			i += 1
		
		#  Display calculated result
		self.printPolynomial(z, n)
	

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

if __name__ == "__main__": main()

Output

7 + 8 x^ 1 + 6 x^ 2 + 1 x^ 3 + 3 x^ 4
5 + 1 x^ 1 + 3 x^ 2 + 2 x^ 3
12 + 9 x^ 1 + 9 x^ 2 + 3 x^ 3 + 3 x^ 4
#    Ruby program
#    Add of two polynomials using array
class Addition 
	#  Display polynomial sequence
	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 maxLength(a, b) 
		if (a > b) 
			return a
		end

		return b
	end

	def addPolynomials(x, y, a, b) 
		#  Display polynomial
		self.printPolynomial(x, a)
		self.printPolynomial(y, b)
		#  Get max length
		n = self.maxLength(a, b)
		#  Use to collect result element
		z = Array.new(n) {0}
		i = 0
		while (i < n) 
			if (i < a && i < b) 
				#  Case ➀ : When have both polynomials element exists
				z[i] = x[i] + y[i]
			elsif (i < a) 
				#  Case ➁ : When have x polynomial element exists
				z[i] = x[i]
			else
 
				#  Case ➂ : When have y polynomial element exists
				z[i] = y[i]
			end

			i += 1
		end

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

end

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

main()

Output

7 + 8x^1 + 6x^2 + 1x^3 + 3x^4
5 + 1x^1 + 3x^2 + 2x^3
12 + 9x^1 + 9x^2 + 3x^3 + 3x^4
/*
    Scala program
    Add of two polynomials using array
*/
class Addition()
{
	// Display polynomial sequence
	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 maxLength(a: Int, b: Int): Int = {
		if (a > b)
		{
			return a;
		}
		return b;
	}
	def addPolynomials(x: Array[Int], y: Array[Int], 
      				   a: Int, b: Int): Unit = {
		// Display polynomial
		printPolynomial(x, a);
		printPolynomial(y, b);
		// Get max length
		var n: Int = maxLength(a, b);
		// Use to collect result element
		var z: Array[Int] = Array.fill[Int](n)(0);
		var i: Int = 0;
		while (i < n)
		{
			if (i < a && i < b)
			{
				// Case ➀ : When have both polynomials element exists
				z(i) = x(i) + y(i);
			}
			else if (i < a)
			{
				// Case ➁ : When have x polynomial element exists
				z(i) = x(i);
			}
			else
			{
				// Case ➂ : When have y polynomial element exists
				z(i) = y(i);
			}
			i += 1;
		}
		// Display calculated result
		printPolynomial(z, n);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Addition = new Addition();
		// 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.addPolynomials(x, y, a, b);
	}
}

Output

7 + 8x^1 + 6x^2 + 1x^3 + 3x^4
5 + 1x^1 + 3x^2 + 2x^3
12 + 9x^1 + 9x^2 + 3x^3 + 3x^4
import Foundation;
/*
    Swift 4 program
    Add of two polynomials using array
*/
class Addition
{
	// Display polynomial sequence
	func printPolynomial(_ polynomial: [Int], _ n: Int)
	{
		var i: Int = 0;
		while (i < n)
		{
			if (i  != 0)
			{
				print(" +", polynomial[i] ,"x^", i, terminator: "");
			}
			else
			{
				print(polynomial[i], terminator: "");
			}
			i += 1;
		}
		print(terminator: "\n");
	}
	func maxLength(_ a: Int, _ b: Int) -> Int
	{
		if (a > b)
		{
			return a;
		}
		return b;
	}
	func addPolynomials(_ x: [Int], _ y: [Int], _ a: Int, _ b: Int)
	{
		// Display polynomial
		self.printPolynomial(x, a);
		self.printPolynomial(y, b);
		// Get max length
		let n: Int = self.maxLength(a, b);
		// Use to collect result element
		var z: [Int] = Array(repeating: 0, count: n);
		var i: Int = 0;
		while (i < n)
		{
			if (i < a && i < b)
			{
				// Case ➀ : When have both polynomials element exists
				z[i] = x[i] + y[i];
			}
			else if (i < a)
			{
				// Case ➁ : When have x polynomial element exists
				z[i] = x[i];
			}
			else
			{
				// Case ➂ : When have y polynomial element exists
				z[i] = y[i];
			}
			i += 1;
		}
		// Display calculated result
		self.printPolynomial(z, n);
	}
}
func main()
{
	let task: Addition = Addition();
	// 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.addPolynomials(x, y, a, b);
}
main();

Output

7 + 8 x^ 1 + 6 x^ 2 + 1 x^ 3 + 3 x^ 4
5 + 1 x^ 1 + 3 x^ 2 + 2 x^ 3
12 + 9 x^ 1 + 9 x^ 2 + 3 x^ 3 + 3 x^ 4
/*
    Kotlin program
    Add of two polynomials using array
*/
class Addition
{
	// Display polynomial sequence
	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 maxLength(a: Int, b: Int): Int
	{
		if (a > b)
		{
			return a;
		}
		return b;
	}
	fun addPolynomials(x: Array < Int > , y: Array < Int > , 
                       a: Int, b: Int): Unit
	{
		// Display polynomial
		this.printPolynomial(x, a);
		this.printPolynomial(y, b);
		// Get max length
		val n: Int = this.maxLength(a, b);
		// Use to collect result element
		val z: Array < Int > = Array(n)
		{
			0
		};
		var i: Int = 0;
		while (i < n)
		{
			if (i < a && i < b)
			{
				// Case ➀ : When have both polynomials element exists
				z[i] = x[i] + y[i];
			}
			else if (i < a)
			{
				// Case ➁ : When have x polynomial element exists
				z[i] = x[i];
			}
			else
			{
				// Case ➂ : When have y polynomial element exists
				z[i] = y[i];
			}
			i += 1;
		}
		// Display calculated result
		this.printPolynomial(z, n);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Addition = Addition();
	// 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.addPolynomials(x, y, a, b);
}

Output

7 + 8x^1 + 6x^2 + 1x^3 + 3x^4
5 + 1x^1 + 3x^2 + 2x^3
12 + 9x^1 + 9x^2 + 3x^3 + 3x^4


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.

New Comment







© 2021, kalkicode.com, All rights reserved