-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpson3~8.c
More file actions
42 lines (34 loc) · 835 Bytes
/
Copy pathSimpson3~8.c
File metadata and controls
42 lines (34 loc) · 835 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <stdio.h>
#include <math.h>
float func(float x)
{
return x * x + 1;
}
int main()
{
float a, b, h, n, sum = 0, x;
printf("enter the lower bound: ");
scanf("%f", &a);
printf("enter the upper bound: ");
scanf("%f", &b);
printf("enter the number of sub internval: ");
scanf("%f", &n);
h = (b - a) / n;
for (int i = 1; i < n; i++)
{
x = a + i * h;
if (i % 3 == 0)
{
sum = sum + 2 * func(x);
}
else
{
sum = sum + 3 * func(x);
}
}
sum = ((3 * h / 8) * (func(a) + func(b) + sum));
printf("Simpson 3/8 Rule - - - - - - - - - -\n");
printf("Integral value = %.6lf\n", sum);
printf("- - - - - - - - - - - - - - - - - - -\n");
return 0;
}