Simpson’s 1/3 Rule

Source Code:

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

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

Output