Trapezoidal rule in C

#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x)
{
    return(1/(1+pow(x,2)));
}
void main()
{
    int i,n;
    float x0,xn,h,y[20],so,se,ans,x[20];
    printf(" \n\t\t \tTech Apurba .... Crazy Coders\n\n");
    printf("\n Enter values of x0,xn,h:\n");
    scanf("%f%f%f",&x0,&xn,&h);
    n=(xn-x0)/h;
    if(n%2==1)
    {
        n=n+1;
    }
    h=(xn-x0)/n;
    printf("\nRefined value of n is:  %d \n\n",n);
    printf("Refined value of h is : %f ",h);
    printf("\n\n \tY values are\n");
    for(i=0; i<=n; i++)
    {
        x[i]=x0+i*h;
        y[i]=f(x[i]);
        printf("\n%f\n",y[i]);
    }
    so=0;
    se=0;
    for(i=1; i<n; i++)
    {
        if(i%2==1)
        {
            so=so+y[i];
        }
        else
        {
            se=se+y[i];
        }
    }
    ans=h/3*(y[0]+y[n]+4*so+2*se);
    printf("\n The Final Result is %f",ans);
    getch();
}


OUTPUT :-




For Other Programs Visit The WebSite:-   https://www.techapurba.com/

Follow Me On Social Media :-

Instagram link :- https://www.instagram.com/apurba_khanra/
INSTA photography page :- https://www.instagram.com/photo_mania_hub/
Twitter link : - https://twitter.com/ApurbaKhanra199
Facebook page link (Crazy Coders) :-https://www.facebook.com/codercrazy/?modal=admin_todo_tour

Insta photography link :- https://www.instagram.com/photo_mania_hub/

Facebook link :- https://www.facebook.com/profile.php?id=100009747586288
FB photography page :-  https://www.facebook.com/photomaniahub/?modal=admin_todo_tour

For tech related videos visit my other website :- https://apurbatechinfo.blogspot.com/

Post a Comment

0 Comments