#include <stdio.
h>
#include <math.h> // For functions like pow() or sin(), cos() if needed
// Define the function you want to integrate
// You can change this function as per your requirement
double f(double x) {
return x * x; // Example: Integrating x^2
// return sin(x); // Example: Integrating sin(x)
// return 2 * x + 3; // Example: Integrating 2x + 3
int main() {
double a, b; // Lower and upper limits of integration
int n; // Number of trapezoids (or subintervals)
double h; // Width of each trapezoid (h = (b - a) / n)
double integral; // To store the calculated integral
int i; // Loop counter
printf("Enter the lower limit of integration (a): ");
scanf("%lf", &a);
printf("Enter the upper limit of integration (b): ");
scanf("%lf", &b);
printf("Enter the number of subintervals (n) for approximation: ");
scanf("%d", &n);
// Calculate the width of each subinterval
h = (b - a) / n;
// Initialize integral with the first and last terms
// which are f(a) and f(b) multiplied by 0.5 * h (h/2)
integral = (f(a) + f(b)) / 2.0;
// Sum the areas of the trapezoids in between
for (i = 1; i < n; i++) {
integral += f(a + i * h);
integral *= h; // Multiply the sum by h
printf("\n--- Integral Calculation ---\n");
printf("Function being integrated: f(x) = x^2 (as defined in the code)\
n");
printf("Lower limit (a): %.4lf\n", a);
printf("Upper limit (b): %.4lf\n", b);
printf("Number of subintervals (n): %d\n", n);
printf("Approximate integral value: %.6lf\n", integral);
return 0;