#include <iostream.h>
#include <conio.h>
#include <math.h>
double X[5],Y[5];
double langrange(double a, int n)
{
double pi, L;
int i,j;
//double X[5], Y[5];
L=0;
for (i=1;i<=n;i++)
{
pi=1;
for (j=1;j<=n;j++)
{
if (i!=j)
pi=pi*(a-X[j])/(X[i]-X[j]);
}
L=L+Y[i]*pi;
}
return L;
}
void main()
{
int i,j,n;
double p;//, X[5], Y[5];
clrscr();
cout<< "n= ";
cin>>n;
for(i=1;i<=n;i++)
{
cout <<"X["<<i<<"]=";
cin>>X[i];
}
for(i=1;i<=n;i++)
{
cout <<"Y["<<i<<"]=";
cin>>Y[i];
}
cout<<"p=";
cin >>p;
cout<<"langrange(" <<p <<","<<n<<")="<<langrange(p,n);
getch();
}