Logo 
Search:

C Programming Article

Submit Article
Comments on Program to compute area under a curve

Write a program to compute area under a curve.


Alpha Bet
Alpha Bet from AustraliaJun 02
why is it giving output as zero???

// program to calculate area under f(x) = x*x+1
#include<stdio.h>
float f_l(float);
float area(float, float, int);
int main (void)
{
float ll, ul; // ll-lower limit ul-upper limit
int n; // no of trapezoids
printf("enter lower limit and upper limit\t",ll,ul);
scanf("%f %f",&ll,&ul);
printf("enter no of trapezoids\t");
scanf("%d",&n);
printf("total area = %f",area(ll, ul,n));
return 0;
}
float area(float x, float y,int p)
{
float a1=0,a2=0,b;
int k;
b = y/p;
for (k=0;k<p;k++)
a1 += ((f_l(b*k)+f_l((b*(k+1))))*b*(1/2));
b = x/p;
for (k=0;k<p;k++)
a2 += ((f_l(b*k)+f_l((b*(k+1))))*b*(1/2));
return (a1-a2);
}

float f_l(float g)
{
return (g*g + 1);
}
Ms Leeli
Ms Leeli from Brunei DarussalamApr 13
it doesnt work in turbo c++
can you give the program in turbo c++ with the same output

Output

AREA UNDER A CURVE
Enter lower limit: 0
Enter upper limit: 3
Enter number of trapezoids: 30
TOTAL AREA = 12.005000

AREA UNDER A CURVE
Enter lower limit: 0
Enter upper limit: 3
Enter number of trapezoids: 100
TOTAL AREA = 12.000438





for example


#include<stdio.h>
main()

...........


Please enter your Comment
  • Comment should be atleast 15 Characters.
  • Please put code inside [Code] your code [/Code].