Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- double f(double k){
- return 1/(1+k);
- }
- int main(){
- int n,i;
- double a,b,h,x,sum=0,integral=0;
- printf("\nEnter the no. of sub-intervals(MULTIPLE OF 3): ");
- scanf("%d",&n);
- printf("\nEnter the initial limit: ");
- scanf("%lf",&a);
- printf("\nEnter the final limit: ");
- scanf("%lf",&b);
- h=(b-a)/n;
- //cout<<h<<f(a)<<f(b);
- sum=f(a)+f(b);
- for(i=1;i<n;i++){
- x=a+i*h;
- if(i%3==0){
- sum=sum+2*f(x);
- }
- else{
- sum=sum+3*f(x);
- }
- }
- integral=sum*((3*h)/8);
- printf("\nThe integral is: %lf\n",integral);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement