C++ Program of Simpson’s 3/8th Rule for the Evaluation of Definite Integrals

//Simpson's 3/8th Rule for Evaluation of Definite Integrals 
using namespace std; 
double f(double x) {     
    double a=1/(1+x*x);    //write the function whose definite integral is to be calcuated here     
    return a; 
int main() {
    cout.precision(4);        //set the precision
    int n,i;            //n is for subintervals and i is for loop
    double a,b,c,h,sum=0,integral;
    cout<<"\nEnter the limits of integration,\n\nInitial limit,a= ";
    cout<<"\nFinal limit, b=";  //get the limits of integration
    cout<<"\nEnter the no. of subintervals(IT SHOULD BE A MULTIPLE OF 3), \nn=";
    //get the no. of subintervals
    double x[n+1],y[n+1];
    h=(b-a)/n;     //get the width of the subintervals
    for (i=0;i<n+1;i++){                        //loop to evaluate x0,...xn and y0,...yn
        x[i]=a+i*h;                //and store them in arrays
    for (i=1;i<n;i++)     {
        if (i%3==0)
    integral=3*h/8*(y[0]+y[n]+sum);  //3h/8*[y0+yn+3*(y1+y2+y4+...)+2*(y3+y6+y9+...+)]
    cout<<"\nThe definite integral  is "<<integral<<"\n"<<endl;
    return 0;

outpu simpson 38 c++ program

Explanation of the code:

PhD researcher at Friedrich-Schiller University Jena, Germany. I'm a physicist specializing in computational material science. I write efficient codes for simulating light-matter interactions at atomic scales. I like to develop Physics, DFT and Machine Learning related apps and softwares from time to time. Can code in most of the popular languages. Like to share my knowledge in Physics and applications using this Blog and a YouTube channel.
[wpedon id="7041" align="center"]

One thought on “C++ Program of Simpson’s 3/8th Rule for the Evaluation of Definite Integrals

Leave a Reply

Your email address will not be published. Required fields are marked *