Simpson’s 3/8 Rule

Source Code:

Write a program in C to find the value of using the Simpson’s 3/8 Rule with 𝒏 = 4

#include <stdio.h>
#include <conio.h>
double f(double x){ return x*x + 1; }
int main(){
    clrscr();
    int n=3; double a=0,b=1,h,sum1=0,sum2=0;
    h=(b-a)/n;
    for(int i=1;i<n;i++){
    if(i%3==0) sum2+=f(a+i*h);
    else sum1+=f(a+i*h);
    }
    double I=(3*h/8)*(f(a)+f(b)+3*sum1+2*sum2);
    printf("\n\n\tIntegral : %.4f\n",I);
    getch();
    return 0;
}

Output