# Solve Trapezoidal Rule C Program

## C Program To Solve Trapezoidal Rule

Learn How To Implement Trapezoidal Rule in C Programming Language. Find out the easiest way to write a C Program for Trapezium Rule Implementation. It is a method to approximately calculate the definite integral.

#### Trapezium Rule Formula

This is a technique to approximate the definite integrals. It approximates the region under the graph of the function **f(x)** and calculates its area. This method becomes more accurate and outputs perfect results when periodic functions are integrated over their periods.

**Must Read: C Program For Simpson’s Rule**

If you try to compile this C Program For Trapezium Rule in Linux, you will get the following error:

1 2 3 | /tmp/ccgL393M.o: In function `trapezoidal_rule': test.c:(.text+0x13): undefined reference to `sqrt' collect2: error: ld returned 1 exit status |

This is because the pow() method cannot be found in the library files. To overcome this error, you will have to explicitly include the math.h header file. Compile the program using the following command:

1 | gcc test.c -lm |

#### C Program To Solve Trapezoidal Rule using Function

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 | #include<stdio.h> #include<math.h> float trapezoidal_rule(float x) { return sqrt(x); } int main() { int count, interval; float lower_limit, upper_limit, result = 0, sum = 0, length; printf("\nEnter the Intervals:\t"); scanf("%d", &interval); printf("\nEnter Lower Limit:\t"); scanf("%f", &lower_limit); printf("\nEnter Upper Limit:\t"); scanf("%f", &upper_limit); length = (upper_limit - lower_limit) / interval; for(count = 1; count <= interval - 1; count++) { sum = sum + trapezoidal_rule(lower_limit + count*length); } result = (trapezoidal_rule(lower_limit) + trapezoidal_rule(upper_limit) + 2*sum) * (length/2); printf("\nTrapezoidal Rule Value:\t%f\n", result); return 0; } |

**Must Read: C Program To Display India’s Map**

#### Output

If you have any compilation errors or doubts in this C Program To Implement Trapezoidal Rule, let us know about in the Comment Section below.

Is it necessary to use sqrt() function in another separate method in this trapezium theorem in c programming?

No, it is not required. You can include it within the main() method. But, then the complexity of the main method woukd increase and it will be difficult to understand this c program.

This trapezium theorem is just so simple and easily understandable. Thanks.