# Lagrange’s Interpolation Formula

The java implementation of Lagrange’s Interpolation Formula

```import java.util.*;

public class Lagrange {

public static double xToY (double x [], double y [], double xx) {

int i, j , size = x.length;
double  sum = 0, term;

for(i = 0; i < size; ++i) {
term = y[i];
for(j = 0; j < size; ++j) {
if( i != j)
term *= (xx - x [j]) / (x[i] - x[j]);
}
sum += term;
}
return sum;
}

public static double yToX (double x1 [], double y1 [], double yy) {

int i, j, size = x1.length;
double term,sum = 0 ;

for(i = 0; i < size; ++i){
term = x1[i];
for(j = 0; j < size; ++j){
if(i != j)
term *= (yy - y1[j]) / (y1[i] - y1[j]);
}
sum += term ;
}
return sum;
}

public static void main (String args []) {
Scanner sc = new Scanner (System.in);

/* For finding yy */
double x [] = {3 , 4, 5, 6, 7, 8, 9};
double y [] = {0 , 1, 3, 7, 13, 22, 34};
double xx = 10;

System.out.println(xToY(x, y, xx));
System.out.println();

/* For finding xx */
double x1 [] = {0.46, 0.47, 0.48, 0.49};
double y1 [] = {0.4846555, 0.4937452, 0.5027498, 0.5116683};
double yy = 0.5;

System.out.println(yToX(x1, y1, yy));
System.out.println();

}
}
```
1. Jayakumar J says: