EX.NO:2 ADDITION OF TWO POLYNOMIAL.
AIM:
To write a program for the addition of polynomial.
ALGORITHM:
Step 1:Start the program.
Step 2:Declare the variables,structure and functions to be used.s
Step 3:In the main create two polynomial poly1 and poly2 to perform
addition and then display the result.
Step 4:stop the program.
CREATE
*Get the size of the list and the two polynomial coefficient
and the power.
*Display the both polynomial.
SHOW
*Display the two polynomial input such as poly1 and poly2 in the
Form
x^n+x^2+x^1+c
ADD
*Check that the power ofpoly1 is greater than power of poly2.
*If greater ,then the power and coefficient of poly1 is assigned to the
result poly.
*If it is less,then the power and coefficient of poly2 is assigned to the
result poly.
*If the powers of poly1 and poly2 are equal then the addition of the
two coefficient is performed.
*If any leftout polynomial is found in the input then assign the
polynomial to the output.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct link
{
int coeff;
int pow;
struct link *next;
};
struct link*poly1=NULL,*poly2=NULL,*poly=NULL;
void create(struct link*node)
{
char ch;
int i,n;
printf("\n Enter the size of the list:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter the coeff:");
scanf("%d",&node->coeff);
printf("\nEnter power:");
scanf("%d",&node->pow);
node->next=(struct link*)malloc(sizeof(struct link));
node=node->next;
node->next=NULL;
}
}
void show(struct link*node)
{
while(node->next!=NULL)
{
printf("%dx^%d",node->coeff,node->pow);
node=node->next;
if(node->next!=NULL)
printf("+");
}
}
void polyadd(struct link*poly1,struct link *poly2,struct link *poly)
{
while(poly1->next&&poly2->next)
{
if(poly1->pow>poly2->pow)
{
poly->pow=poly1->pow;
poly->coeff=poly1->coeff;
poly1=poly1->next;
}
else
if(poly1->pow<poly2->pow)
{
poly->pow=poly2->pow;
poly->coeff=poly2->coeff;
poly2=poly2->next;
}
else
{
poly->pow=poly1->pow;
poly->coeff=poly1->coeff+poly2->coeff;
poly1=poly1->next;
poly2=poly2->next;
}
poly->next=(struct link*)malloc(sizeof(struct link));
poly=poly->next;
poly->next=NULL;
}
while(poly1->next||poly2->next)
{
if(poly1->next)
{
poly->pow=poly1->pow;
poly->coeff=poly1->coeff;
poly1=poly1->next;
}
if(poly2->next)
{
poly->pow=poly2->pow;
poly->coeff=poly2->coeff;
poly2=poly2->next;
}
poly->next=(struct link*)malloc(sizeof(struct link));
poly=poly->next;
poly->next=NULL;
}
}
void main()
{
clrscr();
char ch;
do
{
poly1=(struct link*)malloc(sizeof(struct link));
poly2=(struct link*)malloc(sizeof(struct link));
poly=(struct link*)malloc(sizeof(struct link));
printf("*****POLYNOMIAL ADDITION*****");
printf("\n\n");
printf("\nEnter 1st polynomial p1(x):");
create(poly1);
printf("\nEnter 2nd polynomial p2(x):");
create(poly2);
clrscr();
printf("\n 1st number:");
show(poly1);
printf("\n 2nd number:");
show(poly2);
polyadd(poly1,poly2,poly);
printf("\n\n Added polynimial:");
show(poly);
printf("\n\n Doyou want to contiue(Y/N):");
ch=getch();
}
while(ch=='y'||ch=='y');
}