0% found this document useful (0 votes)
8 views2 pages

Polynomial Addition in C Programming

The document is a C program that defines a structure for polynomials and includes functions to read, add, and print polynomials. It allows users to input two polynomials and outputs their sum. The program utilizes arrays of structures to manage polynomial terms and their coefficients and exponents.

Uploaded by

Karthikeyan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

Polynomial Addition in C Programming

The document is a C program that defines a structure for polynomials and includes functions to read, add, and print polynomials. It allows users to input two polynomials and outputs their sum. The program utilizes arrays of structures to manage polynomial terms and their coefficients and exponents.

Uploaded by

Karthikeyan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

#include<stdio.

h>
#include<conio.h>
typedef struct poly
{
int coeff;
int expo;
}p;
p p1[10],p2[10],p3[10];
void main()
{
int t1,t2,t3;
int read(p p1[10]);
int add(p p1[10],p p2[10],int t1,int t2,p p3[10]);
void print(p p2[10],int t2);
clrscr();
t1=read(p1);
print(p1,t1);
t2=read(p2);
print(p2,t2);
t3=add(p1,p2,t1,t2,p3);
print(p3,t3);
getch();
}
int read(p p[10])
{
int t1,i;
printf("\n enter the total:no:of:terms in the poly \n");
scanf("%d",&t1);
printf("\n enter the coeff and expo in the dscending order \n");
for(i=0;i<t1;i++)
scanf("%d %d",&p[i].coeff,&p[i].expo);
return(t1);
}
int add(p p1[10],p p2[10],int t1,int t2,p p3[10])
{
int i,j,k;
int t3;
i=0;
j=0;
k=0;
while(i<t1&&j<t2)
{
if(p1[i].expo==p2[j].expo)
{
p3[k].coeff=p1[i].coeff+p2[j].coeff;
p3[k].expo=p1[i].expo;
i++;
j++;
k++;
}
else if(p1[i].expo>p2[j].expo)
{
p3[k].coeff=p1[j].coeff;
p3[k].expo =p1[j].expo;
i++;
k++;
}
else
{
p3[k].coeff=p2[j].coeff;
p3[k].expo=p2[j].expo;
j++;
k++;
}
}
while(i<t1)
{
p3[k].coeff=p1[i].coeff;
p3[k].expo=p1[i].expo;
i++;
k++;
}
while(j<t2)
{
p3[k].coeff=p2[j].coeff;
p3[k].expo=p2[j].expo;
j++;
k++;
}
t3=k;
return(t3);
}
void print(p pp[10],int term)
{
int k;
printf("\n printing the polynomial \n");
for(k=0;k<term-1;k++)
printf("%dx^%d+",pp[k].coeff,pp[k].expo);
printf("%dx^%d",pp[k].coeff,pp[k].expo);
}

You might also like