0% found this document useful (0 votes)
5 views

Polynomial_Addition

This is a program used to add to given polynomials. Program is written in C language.

Uploaded by

Madhira Srinivas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Polynomial_Addition

This is a program used to add to given polynomials. Program is written in C language.

Uploaded by

Madhira Srinivas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

/* This program adds given two polynomials and prints them */

//This program was executed in Embarcadero Dev C++ 6.3 editor (freeware) on Windows 7 OS

#include<stdio.h>
#include<stdlib.h>

//definition of plynomial node


struct Poly_Node
{
int coeff;
int power;
struct Poly_Node *Next;
};

struct Poly_Node *Head1 = NULL;


struct Poly_Node *Head2 = NULL;
struct Poly_Node *Head3 = NULL;

//declaration of user defined functions


struct Poly_Node * CreateLL(struct Poly_Node *, int, int);
void Display(struct Poly_Node *);
struct Poly_Node * AddPoly(struct Poly_Node *, struct Poly_Node *, struct Poly_Node *);

//beginning of main program


int main()
{
int n, co, po, i;
struct Poly_Node *tp;

//reading polynomial-1 details from the user and storing them in a Linked List
printf("\nEnter number of terms in the polynomial-1: ");
scanf("%d", &n);

for(i=1; i<=n; i++)


{
printf("\nEnter, co-efficient and power of %d term of Polynomial-1: ", i);
scanf("%d %d", &co, &po);
Head1 = CreateLL(Head1, co, po);
}

//reading polynomial-2 details from the user and storing them in a Linked List
printf("\nEnter number of terms in the polynomial-2: ");
scanf("%d", &n);

for(i=1; i<=n; i++)


{
printf("\nEnter, co-efficient and power of %d term of Polynomial-2: ", i);
scanf("%d %d", &co, &po);
Head2 = CreateLL(Head2, co, po);
}

printf("\n\n The polynomial-1 is: \n");


Display(Head1);

printf("\n\n The polynomial-2 is: \n");


Display(Head2);

//Adding two polynomials


Head3 = AddPoly(Head1, Head2, Head3);

printf("\n\n The Resultant polynomial is: \n");


Display(Head3);

return 0;
}

//Definition of CreateLL() function. It creates a node,stores term details in the node


//and adds this node at the end of Linked list
struct Poly_Node * CreateLL(struct Poly_Node *Head, int co, int po)
{
struct Poly_Node *tp, *lp;

//create a new node


tp = (struct Poly_Node *)malloc(sizeof(struct Poly_Node));

//store details of term of a polinomial


tp->coeff = co;
tp->power = po;
tp->Next = NULL;

//add this node at the end of the linked list


if(Head == NULL) //LL is empty, add new node as 1st node
{
Head = tp;
}
else //LL is not empty so, add new node at the end of LL
{
//locate last node of LL
lp = Head;
while(lp->Next != NULL)
{
lp = lp->Next;
}

//now add new node at the end of LL


lp->Next = tp;
}

return Head;
}
//Definition of display() function. It will display details of polynomial
void Display(struct Poly_Node *Head)
{
struct Poly_Node *tp;
tp = Head;

while(tp != NULL)
{
if(tp->coeff > 0)
{
printf("+");
}
printf("%dx(%d)", tp->coeff, tp->power);
tp = tp->Next;
}
}

//Definition of AddPoly() function. It adds two given polynomials and stores the result
//in third polynomial
struct Poly_Node * AddPoly(struct Poly_Node *Head1, struct Poly_Node *Head2, struct Poly_Node
*Head3)
{
struct Poly_Node *tp1, *tp2;
int co, po;

//Adding two polinomials by comparing the powers of each term in two polinomials
tp1 = Head1;
tp2 = Head2;

while(tp1 != NULL && tp2 != NULL)


{
if(tp1->power == tp2->power) //powers are same add co-efficients
{
co = tp1->coeff + tp2->coeff;
po = tp1->power;
Head3 = CreateLL(Head3, co, po);
tp1 = tp1->Next;
tp2 = tp2->Next;
}
else if(tp1->power > tp2->power) //add 1st poly term into 3rd poly
{
co = tp1->coeff;
po = tp1->power;
Head3 = CreateLL(Head3, co, po);
tp1 = tp1->Next;
}
else //add 2nd poly term into 3rd poly
{
co = tp2->coeff;
po = tp2->power;
Head3 = CreateLL(Head3, co, po);
tp2 = tp2->Next;
}
}
//loop terminates when one of the polynomials are over so, copy remaining terms of
//another polynomial into 3rd polynomial

if(tp1 != NULL) //1st ploy is not completed so, copy its remaining terms into 3rd poly
{
while(tp1 != NULL)
{
co = tp1->coeff;
po = tp1->power;
Head3 = CreateLL(Head3, co, po);
tp1 = tp1->Next;
}
}
if(tp2 != NULL) //2ndt ploy is not completed so, copy its remaining terms into 3rd poly
{
while(tp2 != NULL)
{
co = tp2->coeff;
po = tp2->power;
Head3 = CreateLL(Head3, co, po);
tp2 = tp2->Next;
}
}
return Head3;
}

Output:
Enter number of terms in the polynomial-1: 4
Enter, co-efficient and power of 1 term of Polynomial-1: 5 4
Enter, co-efficient and power of 2 term of Polynomial-1: -3 2
Enter, co-efficient and power of 3 term of Polynomial-1: 9 1
Enter, co-efficient and power of 4 term of Polynomial-1: -6 0

Enter number of terms in the polynomial-2: 3


Enter, co-efficient and power of 1 term of Polynomial-2: 3 3
Enter, co-efficient and power of 2 term of Polynomial-2: -2 1
Enter, co-efficient and power of 3 term of Polynomial-2: 9 0

The polynomial-1 is:


+5x(4)-3x(2)+9x(1)-6x(0)

The polynomial-2 is:


+3x(3)-2x(1)+9x(0)

The Resultant polynomial is:


+5x(4)+3x(3)-3x(2)+7x(1)+3x(0)

You might also like