You are given a polynomial of degree n. The polynomial is of the form P(x) = anxn + an-1xn-1 + … + a0, where the ai‘s are the coefficients. Given an integer x, write a program that will evaluate P(x).
SOLUTION:
#include<stdio.h> /* function to calculate power x^y */ int power(int x, int y){ int pow=1; while (y!=0){ pow*=x; y--; } return pow; }
#include<stdio.h> /* function to calculate power x^y */ int power(int x, int y){ int pow=1; while (y!=0){ pow*=x; y--; } return pow; }
int main()
{
int n, x;
int a[10];
scanf("%d %d", &n, &x);
int i, j, sum=0;
for(i=n; i>=0; i--)
{
scanf("%d", &a[i]);
}
int temp=0;
for(j=n; j>=0; j--)
{
temp=a[j]*power(x,j);
sum=sum+temp;
}
printf("%d", sum);
return 0;
}