Numerical Integration
CSC-212 · Semester III · Numerical Methods
#include <stdio.h> #include <math.h> double f(double x) { return x * x; } int main() { double a = 0, b = 1; int n = 10; double h = (b - a) / n; double sum = (f(a) + f(b)) / 2; for (int i = 1; i < n; i++) { sum += f(a + i * h); } double integral = h * sum; printf("Integral of x^2 over [0,1] = %.6f\n", integral); printf("Exact value = %.6f\n", 1.0 / 3); return 0; }
Write some code, then press Run. Stdout and errors appear here.