Write a C program to implement Simpson method.

Algorithm:

Step 1.  Read x1, x2, e
Step 2.  h = (x2 - x1)/2
Step 3.  i = 2
Step 4.  si = f(x1) + f(x2)
Step 5.  s2 = 0
Step 6.  s4 = f(x1 + h)
Step 7.  I0 = 0
Step 8.  In = (s + 4s4).(h/3)

Repeat

Step 9.  s2 = s2 + s4 { s2 stores already computed functional value and s4 the value computed in the new nitration }
Step 10. s4 = 0
Step 11. x = x1 + h/2
Step 12. for j = 1 to I do
Step 13. s4 = s4 + f(x)
Step 14. x = x + h
Step 15. h = h/2
Step 16. i = 2i
Step 17. io = in
Step 18. in = (s1 + 2s2 + 4s4) . (h/3)
Step 19. until |In-Io|≤e. /in
Step 20. Write In, h, i
Step 21. STOP

Flowchart:

Program:

#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
 float h, a, b, n, x[20], y[20], sum = 0, itgl;
 int i;
 clrscr();
 printf("enter the values of a, b, n");
 scanf("%f%f%f", &a, &b, &n);
 printf("enter the values of x");
 for(i = 0; i <= n; i++)
 {
  scanf("%f", &x[i]);
 }
 printf("\n enter the values of y");
 for(i = 0; i <= n; i++)
 {
  scanf("%f", &y[i]);
 }
 h = (b - a)/n;
 a = x[0];
 b = x[n];
 for(i = 0; i <= (n-2); i++)
 {
  x[i] = x[i] + h;
  if(i % 2 == 0)
  {
   sum = sum + 4 * y[i];
  }
  else
  {
   sum = sum + 2 * y[i];
  }
 }
 itgl = sum * (h/3);
 printf("integral value%f", itgl);
 getch();
}

Input & Output:

enter the values of a, b, n
123
enter the value of x
4567
enter the values of y
8912
integral value is 5.555556