Code
/* simpson's (1/3) Rule */
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main ( )
{
float f (float x); /* function prototype */
float a, b , h, sum = 0.0 , result;
int i , n;
clrscr ();
printf(" Enter lower limit of the integral:");
scanf("%f", &a);
printf("\n Enter upper limit of the intgral :");
scanf("%f" , &b);
printf("\n Enter the number of segments:");
scanf("%d", &n);
if(n % 2!= 0)
printf("\n Number of segments not even number\n");
else
{
h=(b-a)/n;
sum =f(a)+f(b); /*Function call */
for(i=1; i<n; i++)
{
if(i %2 ==0)
sum = sum +2.0*f(a+i*h);/*function call*/
else
sum = sum + 4.0*f(a+i*h);/*function call*/
}
result =(h/3.0)* sum;
printf("\n\ Value of integral is = %f\n\n ", result );
}
getch( );
}
/* function definition f( )*/
float f ( float x)
{
return(1.0/(1.0 + x*x)); /* integrand */
}