import
java.text.*;
import
java.math.*;
class
GFG{
static
float
proterm(
int
i,
float
value,
float
x[])
{
float
pro =
1
;
for
(
int
j =
0
; j < i; j++) {
pro = pro * (value - x[j]);
}
return
pro;
}
static
void
dividedDiffTable(
float
x[],
float
y[][],
int
n)
{
for
(
int
i =
1
; i < n; i++) {
for
(
int
j =
0
; j < n - i; j++) {
y[j][i] = (y[j][i -
1
] - y[j +
1
]
[i -
1
]) / (x[j] - x[i + j]);
}
}
}
static
float
applyFormula(
float
value,
float
x[],
float
y[][],
int
n)
{
float
sum = y[
0
][
0
];
for
(
int
i =
1
; i < n; i++) {
sum = sum + (proterm(i, value, x) * y[
0
][i]);
}
return
sum;
}
static
void
printDiffTable(
float
y[][],
int
n)
{
DecimalFormat df =
new
DecimalFormat(
"#.####"
);
df.setRoundingMode(RoundingMode.HALF_UP);
for
(
int
i =
0
; i < n; i++) {
for
(
int
j =
0
; j < n - i; j++) {
String str1 = df.format(y[i][j]);
System.out.print(str1+
"\t "
);
}
System.out.println(
""
);
}
}
public
static
void
main(String[] args)
{
int
n =
4
;
float
value, sum;
float
y[][]=
new
float
[
10
][
10
];
float
x[] = {
5
,
6
,
9
,
11
};
y[
0
][
0
] =
12
;
y[
1
][
0
] =
13
;
y[
2
][
0
] =
14
;
y[
3
][
0
] =
16
;
dividedDiffTable(x, y, n);
printDiffTable(y,n);
value =
7
;
DecimalFormat df =
new
DecimalFormat(
"#.##"
);
df.setRoundingMode(RoundingMode.HALF_UP);
System.out.println(
"\nValue at "
+df.format(value)+
" is "
+df.format(applyFormula(value, x, y, n)));
}
}