(C++) CPP Programs - 17
Friday, December 14, 2007
17. Write a C program to evaluate the given polynomial,
p(x) = anxn + an-1xn-1 + an-2xn-2………….+ a1x + a0, by reading its co-efficient into an array.
[HINT: rewrite the polynomial as
p(x) = a0+x(a1+x(a1+x(a2+x(a3+x(a4+…………..x(an-1+xan)))))
and evaluate the function starting from inner loop.]
/*Polynomial*/
#include
#include
#include
void main()
{
float a[34],psum=0,term,x;
int i,n;
clrscr();
printf("Enter the value for x\n");
scanf("%f",&x);
printf("Enter the n value\n\a");
scanf("%d",&n);
printf("Enter the %d co-efficients\n",n+1);
for(i=0;i < = n;i++)
scanf("%f",&a[i]);
for(i=n;i >= 0;i--)
{
term=a[i]*pow(x,i);
psum+=term;
}
printf("The sum of polynomial of given terms=%f\n",psum);
getch();
}
0 comments:
Post a Comment