Numerical Integration Using Trapezoidal Method C Program

C program for Trapezoidal Rule or Method to find numerical integration. To learn algorithm about Trapezoidal rule follow article Trapezoidal Method Algorithm.


#include<stdio.h>
#include<conio.h>
#include<math.h>

/* Define function here */
#define f(x) 1/(1+pow(x,2))

int main()
{
 float lower, upper, integration=0.0, stepSize, k;
 int i, subInterval;
 clrscr();
 /* Input */
 printf("Enter lower limit of integration: ");
 scanf("%f", &lower);
 printf("Enter upper limit of integration: ");
 scanf("%f", &upper);
 printf("Enter number of sub intervals: ");
 scanf("%d", &subInterval);

 /* Calculation */
 /* Finding step size */
 stepSize = (upper - lower)/subInterval;

 /* Finding Integration Value */
 integration = f(lower) + f(upper);
 for(i=1; i<= subInterval-1; i++)
 {
  k = lower + i*stepSize;
  integration = integration + 2 * f(k);
 }
 integration = integration * stepSize/2;
 printf("\nRequired value of integration is: %.3f", integration);
 getch();
 return 0;
}

Trapezoidal Method C Program Output

Enter lower limit of integration: 0
Enter upper limit of integration: 1
Enter number of sub intervals: 6

Required value of integration is: 0.784

Recommended Readings

  1. Numerical Integration Trapezoidal Method Algorithm
  2. Numerical Integration Using Trapezoidal Method Pseudocode
  3. Numerical Integration Using Trapezoidal Method C Program
  4. Trapezoidal Rule Using C++ with Output
  5. Numerical Integration Using Simpson 1/3 Method Algorithm
  6. Numerical Integration Using Simpson 1/3 Method Pseudocode
  7. Numerical Integration Using Simpson 1/3 Method C Program
  8. Simpson 1/3 Rule Using C++ with Output
  9. Numerical Integration Using Simpson 3/8 Method Algorithm
  10. Numerical Integration Using Simpson 3/8 Method Pseudocode
  11. Numerical Integration Using Simpson 3/8 Method C Program
  12. Simpson 3/8 Rule Using C++ with Output