C Program for Trapezoidal Method


A number of definite integrals need to be solved in applied mathematics, physics and engineering. The manual analytical solution of definite integrals is quite cumbersome and time consuming. So, in this post I have presented source code in C program for Trapezoidal method as one of the computer-programming-based solutions of definite integrals. The techniques of numerical methods are used to solve this equation; it involves a number of calculations and efforts have been made to minimize error in the program.

The trapezium or trapezoidal rule can be used as a way of estimating the area under a curve because the area under a curve is given by integration. So, the trapezoidal rule gives a method of estimating integrals. This is useful when you come across integrals that you don’t know how to evaluate. So, the program for trapezoidal method in C given here is applicable to calculate finite integral or area under a curve.

The basic working principle of the trapezoidal method c program is that trapezoidal method splits the area under the curve into a number of trapeziums. Then, area of each trapezium is calculated, and all the individual areas are summed up to give the total area under the curve which is the value of definite integral.

C program for Trapezoidal method - integral graph

In the above figure, area under the curve between the points x0 and xn is to be determined. In order to determine the area, we divide the total interval   (xn– x0) into ‘n’ small interval each of length ‘h’:

                        h=(xn– x0)/n

After that, the C source code for trapezoidal method uses the following formula to calculate the value of definite integral:

C program for Trapezoidal method - integration formula
Source Code for Trapezoidal Method in C:

The aforementioned source code for trapezoidal method is short and simple to understand. It uses a user defined function to calculate the value of function i.e f(x) =1 /(1 + x2). The variable data type used in the program are integer and float types. There are only three variables to be input to the program – x0 ( initial boundary value), xn ( final boundary value) and h (strip length).


Trapezoidal Method in C - Output
Sample Output

Also see,
Trapezoidal Method MATLAB Program
Trapezoidal Method Algorithm/Flowchart
Numerical Methods Tutorial Compilation

As the C program for Trapezoidal Method is executed, it asks for the value of x0, xn and h. After inputting them, it prints the refined value of n & h, and value of each ‘y’ at each intermediate points as shown in the output screen above. At the end, it prints the value of the define integral. To use this source code for other functions, just modify the return(1/(1+pow(x,2))); part in the code.


  1. Trapezoidal rule function, since this one is wrong.

    a and b bound f(x). n is the number of strips.

    typedef double (*CurveFunction)(double);
    double trap(double a, double b, int n, CurveFunction f)
    double area = 0;
    double sum = f(a) + f(b);
    double h = (b – a) / n;

    for (int x = 1; x < n; ++x) {
    sum += f(x * h + a);
    area = h * sum / 2.0;

    return area;

  2. Sorry but isn’t the formula for trapezoidal integration
    s=h/2(y0+yn+2 (sum of rest of terms)
    what’s written is Simpsons 1/3 rule right?


Please enter your comment!
Please enter your name here