0% found this document useful (0 votes)
37 views2 pages

C Language Program About Integration

This C program calculates the definite integral of a specified function using the trapezoidal rule. Users can input the lower and upper limits of integration, as well as the number of subintervals for approximation. The example function integrated in the code is f(x) = x^2, but it can be modified to integrate other functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views2 pages

C Language Program About Integration

This C program calculates the definite integral of a specified function using the trapezoidal rule. Users can input the lower and upper limits of integration, as well as the number of subintervals for approximation. The example function integrated in the code is f(x) = x^2, but it can be modified to integrate other functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

#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;

You might also like