Write c program for addition of polynomial using array

#include<stdio.h>
#include<conio.h>
#include<math.h>
typedef struct
{
int coeff,pw;
}polyno;
void read(polyno p[],int n);
void print(polyno p[], int n);
int eval(polyno p[], int n, int x);
void main()
{
int n1,x;
polyno p1[10];
printf("\nEnter the number of terms in polynomial: ");
scanf("%d",&n1);
printf("\nEnter polynomial: ");
read(p1,n1);
printf("\nFirst polynomial is: ");
print(p1,n1);
printf("\nEnter the value of x: ");
scanf("%d",&x);
printf("\nValue of first polynomial is: %d ",eval(p1,n1,x));
getch();
}
void read(polyno p[],int n)
{
int i;
for(i=0;i<n;i++)
{
printf("\nEnter the coefficient of term %d: ",i+1);
scanf("%d",&p[i].coeff);
printf("\nEnter power of term %d: ",i+1);
scanf("%d",&p[i].pw);
}
}
void print(polyno p[], int n)
{
int i;
for(i=0;i<n-1;i++)
{
printf("%d[x]^%d+",p[i].coeff,p[i].pw);
}
printf("%d[x]^%d",p[i].coeff,p[i].pw);
}
int eval(polyno p[], int n, int x)
{
int s=0,i;
for(i=0;i<n;i++)
{
s=s+p[i].coeff*pow(x,p[i].pw);
}
return s;
}


0 Comments