Flow chart of newton divided difference formula:
Start
Input data points
Calculate
Divided
differences
Construct
interpolating
polynomial
Evaluate polynomial at desired
point
Output interpolated
value
end
PROGRAM NO-5
Aim: Write a program to implement Newton’s Divided Difference formula
Program code:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter number of data points: ";
cin >> n;
double x[10], y[10][10];
cout << "Enter data points (x y):\n";
for(int i = 0; i < n; i++) {
cin >> x[i] >> y[i][0];
// Constructing Divided Difference Table
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]) / (x[i+j] - x[i]);
double xp, sum = y[0][0];
cout << "Enter interpolation point: ";
cin >> xp;
double p = 1;
for(int i = 1; i < n; i++) {
p *= (xp - x[i-1]);
sum += y[0][i] * p;
cout << "Interpolated value at " << xp << " is " << sum << endl;
return 0;
Output:
Enter number of data points: 3
Enter data points (x y):
11
24
39
Enter interpolation point: 2.5
Interpolated value at 2.5 is 6.25
Flow char for numerical integration by trapezoidal rule
start
Read a(upper
limit)
Read b(lower limit)
Read n(number
of subintervals)
Yes
no
If(b>a)
yes
yes Upper limit must be
greater than lower
If n>0 no
Number of
subinterval must be
positive
Calculate h=f(a)
+f(b)
Initialize sum=
f(a) + f(b)
Calculate x=a+i*h
Update sum+=2*f(x)
Calculate
result=(h/2)*sum
end
PRACTICAL – 6
AIM - Program for sovling numerical integration by trapezoidal rule
#SOURCE CODE
#include <iostream>
#include <cmath> // for math functions like sin, cos, etc.
using namespace std;
// Define the function to integrate here
double f(double x) {
return x * x; // Example: f(x) = x^2
// Trapezoidal rule implementation
double trapezoidal(double a, double b, int n) {
double h = (b - a) / n;
double sum = f(a) + f(b);
for (int i = 1; i < n; ++i) {
double x = a + i * h;
sum += 2 * f(x);
return (h / 2) * sum;
int main() {
double a, b;
int n;
// Input
cout << "Enter lower limit a: ";
cin >> a;
cout << "Enter upper limit b: ";
cin >> b;
cout << "Enter number of subintervals n: ";
cin >> n;
// Compute integral
double result = trapezoidal(a, b, n);
cout << "Approximate value of the integral: " << result << endl;
return 0;
OUTPUT
Enter lower limit a: 2
Enter upper limit b: 4
Enter number of subintervals n: 2
Approximate value of the integral: 19
Flow chart for numerical integration by Simpson 1/3 rule
start
Read a and b and read
n (number of sub
interval)
no
Is n even
yes
H=(b-a)/n
N must be even
Sum=f(a)+f(b
)
Loop I from 1 to n-1
Calculate x=a+i*n
Add 4*f(x) to sum
Loop I from 2 to n-2
Calculate x=a+i*hd
Add 2*f(x) to sum
Result =(h/3)*sum
end
PRACTICAL – 7
AIM- Program for sovling numerical integration by simpson 1/3 rule
#SOURCE CODE
#include <iostream>
#include <cmath> // for math functions
using namespace std;
// Define the function to integrate here
double f(double x) {
return x * x; // Example: f(x) = x^2
// Simpson's 1/3 rule implementation
double simpson(double a, double b, int n) {
if (n % 2 != 0) {
cout << "Number of subintervals n must be even for Simpson's 1/3 rule.\n";
return NAN;
double h = (b - a) / n;
double sum = f(a) + f(b);
// Sum over odd indices
for (int i = 1; i < n; i += 2) {
double x = a + i * h;
sum += 4 * f(x);
// Sum over even indices
for (int i = 2; i < n; i += 2) {
double x = a + i * h;
sum += 2 * f(x);
return (h / 3) * sum;
}
int main() {
double a, b;
int n;
cout << "Enter lower limit a: ";
cin >> a;
cout << "Enter upper limit b: ";
cin >> b;
cout << "Enter number of subintervals n (even number): ";
cin >> n;
double result = simpson(a, b, n);
if (!isnan(result)) {
cout << "Approximate value of the integral: " << result << endl;
return 0;
OUTPUT
Enter lower limit a: 2
Enter upper limit b: 4
Enter number of subintervals n (even number): 2
Approximate value of the integral: 18.6667
Flow chart for solving numerical integration by Simpson 3/8 rule
start
Read a and b
Read n (no. of
subinterval)
yes
If
n%3==0 no
yes Output
error mssg
H=(b-a)/n
Sum=f(a)+f(b)
Loop I from 1 to n-1
Calculate x=a+i*h
no
Is i%3==0
yes
Sum+=3*f(x)
Result=(3*h/8)*sum
Output approximate
value of iinterval
end
PRACTICAL – 8
AIM- Program for sovling numerical integration by simpson 3/8 rule
#SOURCE CODE
#include <iostream>
#include <cmath>
using namespace std;
// Define the function to integrate here
double f(double x) {
return x * x; // Example function: f(x) = x^2
// Simpson's 3/8 rule implementation
double simpson38(double a, double b, int n) {
if (n % 3 != 0) {
cout << "Number of subintervals n must be a multiple of 3 for Simpson's 3/8 rule.\n";
return NAN;
double h = (b - a) / n;
double sum = f(a) + f(b);
// Sum of terms where i mod 3 != 0 (coeff = 3)
for (int i = 1; i < n; i++) {
double x = a + i * h;
if (i % 3 == 0)
sum += 2 * f(x);
else
sum += 3 * f(x);
return (3 * h / 8) * sum;
int main() {
double a, b;
int n;
cout << "Enter lower limit a: ";
cin >> a;
cout << "Enter upper limit b: ";
cin >> b;
cout << "Enter number of subintervals n (multiple of 3): ";
cin >> n;
double result = simpson38(a, b, n);
if (!isnan(result)) {
cout << "Approximate value of the integral: " << result << endl;
return 0;
OUTPUT
Enter lower limit a: 2
Enter upper limit b: 4
Enter number of subintervals n (multiple of 3): 6
Approximate value of the integral: 18.6667