A day will come when you think yourself safe and happy, and suddenly your joy will turn to ashes in your mouth, and you'll know the debt is paid.

Wednesday, March 11, 2015

How to solve integration using a C program, C program to find integration

Well there's a saying,"Engineering is not complete without Programming" Or did I just made this saying. I thought I didn't. Lord Anish Niroula did.  No matter what Programming and calculus are always pain in the ass for most of the students. 
Here I have written a simple piece of code in C Programming Language which merges the both aspects of calculus and programming. 
This piece of code uses a well known method called "The Trapezium Rule" to calculate the area between the curve. 
Here I have written the code for two forms algebric form and trigonometric form.

The equation for algebric form is y = x^2 + X
The equation for trigonometric form is y = (1+(sinx*sinx))^1/2 

The program for algebric form is :

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
    int a,b,y, area=0, x=0;
    int h=2;
    printf("Enter the value of a \n");
    scanf("%d",&a);
    printf("Enter the value of b \n");
    scanf("%d",&b);
    while (x<b)
    {
        y= x*(x+1);
        area = area+ (h*y);
        x = x+h;
    }
    printf("Total area is : %d \n ",area);
    system("pause");
    return(1);
}

The code for trigonometric form is:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
    int a,b,y, area=0, x=0;
    int h=2;
    printf("Enter the value of a \n");
    scanf("%d",&a);
    printf("Enter the value of b \n");
    scanf("%d",&b);
    while (x<=b)
    {
        y= sqrt(1+(sin(x)*sin(x)));
        area += (h*y);
        x+= h;
    }
    printf("Total area is : %d \n ",area);
    system("pause");
    return(1);
}

                                                           Peace Out Hombres!

0 comments:

Post a Comment

About