#include<iostream>
#include<iomanip>
using namespace std;
#define MAXN 100
int main()
{
float ax[MAXN+1],ay[MAXN+1];
float nr,dr,x,y=0;
int n,i,j;
cout<<"Enter the value of n"<<endl;
cin>>n;
cout<<"Enter the set of values "<<endl;
for(i=0;i<=n;i++)
cin>>ax[i]>>ay[i];
cout<<"Enter the value of x ";
cout<<"for which value of y is wanted"<<endl;
cin>>x;
cout<<fixed;
for(i=0;i<=n;i++)
{
nr=dr=1;
for(j=0;j<=n;j++)
if(j != i)
{
nr *= x-ax[j];
dr *= ax[i]-ax[j];
}
y += (nr/dr)*ay[i];
}
cout<<"When x="<<setw(4)<<setprecision(1)<<x;
cout<<"y="<<setw(7)<<setprecision(1)<<y<<endl;
return 0;
}
Output
Enter the value of n
4
Enter the set of values
5 152
7 390
11 1400
13 2300
17 5200
Enter the value of x for which value of y is wanted
9
When x= 9.0y= 784.4
