#include <iostream>
using namespace std;
int main()
{
int n;
cout << "Enter number of data points: ";
cin >> n;
double x[n], y[n][n];
// Input x values
cout << "Enter x values:\n";
for (int i = 0; i < n; i++)
{
cin >> x[i];
}
// Input y values
cout << "Enter y values:\n";
for (int i = 0; i < n; i++)
{
cin >> y[i][0];
}
// Difference table (nested loop)
for (int j = 1; j < n; j++)
{
for (int i = 0; i < n - j; i++)
{
y[i][j] = y[i + 1][j - 1] - y[i][j - 1];
}
}
// Print difference table (optional)
cout << "\nForward Difference Table:\n";
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n - i; j++)
{
cout << y[i][j] << "\t";
}
cout << endl;
}
double value;
cout << "\nEnter value to interpolate: ";
cin >> value;
double h = x[1] - x[0];
double p = (value - x[0]) / h;
double result = y[0][0];
double term = 1;
// Forward interpolation calculation (another loop)
for (int i = 1; i < n; i++)
{
term = term * (p - (i - 1)) / i;
result += term * y[0][i];
}
cout << "\nInterpolated value at x = " << value << " is: " << result << endl;
return 0;
}