Bisection
#include<stdio.h>
#include<conio.h>
else
#include<math.h>
x1=x3;
float f(float x)
{ x4=x3;
return (pow(x,3)-4*x-9); x3=((x1*f(x2))-(x2*f(x1)))/(f(x2)-f(x1));
} itr++;
int main() printf("\nIn iteration %d Value of x=\t%f",
itr+1,x3);
{
if (fabs(x4-x3)<e)
clrscr();
{
int itr=0, maxitr;
printf("\n\nAfter %d iteration, Root= %f",
float x1,x2,x3,x4,e; itr,x4);
printf("\nEnter value of x0, x1, allowed error getch();
and maximum iteration\n");
return 0;
scanf("%f %f %f %d", &x1, &x2, &e, &maxitr);
}
x3=((x1*f(x2))-(x2*f(x1)))/(f(x2)-f(x1));
}
printf("\n\nIn iteration %d, Value of x3
=\t%f",itr+1,x3); while (itr<maxitr);
do printf("\n\nIteration not sufficient");
{ getch();
if (f(x1)*f(x3)<0) return 1;
x2=x3; }
1
Regula Falsi
#include<stdio.h>
printf("The approximation's value after %d
#include<math.h> iteration is\n",iter,x1);
float F(float x) if(fabs(h)<err)
{ {
return ((x)*(x)-5); root=x1;
} break;
float Fd(float x) }
{ else
return (2*(x)); x0=x1;
} iter++;
int main() }
{ if(root==x1)
float x0,h,err,root,x1; {
int miter,iter; printf("The root is:\n",root);
printf("Enter the first approximation ,the max float fncvalue = F(root);
error and the maximum number of
printf("Value of F(root) is:
iterations\n");
%.12lf",fncvalue);
scanf("%lf%lf%d",&x0,&err,&miter);
}
iter=1;
else
while(iter<=miter)
printf("The unsufficent number of iteration");
{
getch();
h=F(x0)/Fd(x0);
}
x1=x0-h;
2
Matrices
#include<stdio.h> }
#include<conio.h> }
void main() printf("Enter the operation you want to
{ perform\n");
clrscr(); printf("[Link]\n");
int i,g,j,p,q,r,s,l,m,x,y,temp,a1,a2,a3,a4,k1; printf("[Link]\n");
int printf("[Link]\n");
multi[10][10],mult,a[5][5],b[5][5],c[5][5],d[5][5] printf("[Link]\n");
,e[5][5]; scanf("%d",&g);
printf("Enter the no. rows and column of first switch(g)
matrix\n"); {
scanf("%d %d",&a1,&a2); case 1: printf("Addition:\n");
printf("Enter the no. of rows and column of if(a1==a3&&a2==a4)
second matrix\n"); for(p=0;p<a1;p++)
scanf("%d %d",&a3,&a4); {
printf("Enter the first matrix\n"); for(q=0;q<a2;q++)
for(i=0;i<a1;i++) {
{ c[p][q]=a[p][q]+b[p][q];
for(j=0;j<a2;j++) printf("%d",c[p][q]);
{ }
scanf("%d",&a[i][j]); printf("\n");
} }
} break;
printf("Enter the second matrix\n"); case 2: printf("Subtraction:\n");
for(l=0;l<a3;l++) if(i==l&&j==m)
{ for(r=0;r<i;r++)
for(m=0;m<a4;m++) {
{ for(s=0;s<j;s++)
scanf("%d",&b[l][m]); {
3
d[r][s]=a[r][s]-b[r][s]; for(x=0;x<a1;x++)
printf("%d",d[r][s]); {
} for(y=0;y<a2;y++)
printf("\n"); c[y][x]=a[x][y];
} }
break; for(x=0;x<a2;x++)
case 3: printf("Multiplication is \n"); {
if(a2==a3) for(y=0;y<a1;y++)
{ printf("%d",c[x][y]);
for(i=0;i<a1;i++) printf("\n");
{ }
for(j=0;j<a4;j++) break;
{ mult=0; default:
for(k1=0;k1<a3;k1++) printf("Select correct operation");
{ }
mult=mult+a[i][k1]*b[k1][j]; getch();
} }
multi[i][j]=mult;
mult=0;
}}
printf("Product of entered matrices is :
\n");
for(i=0;i<a1;i++){
for(j=0;j<a4;j++){
printf("%d\t",multi[i][j]);
}
printf("\n");
}}
break;
case 4: printf("Transpose is:\n ");
4
Trapezoidal
#include<iostream.h>
sum=sum+f(x);
#include<conio.h>
}
#include<stdio.h>
trap=h*(f(a)+2.0*sum+f(b))/2.0;
#include<math.h>
cout<<"Value of integral with "<<n;
#include<complex.h>
cout<<" Subintervals="<<trap;
float f(float);
getche();
void main()
}
{
float f(float x)
clrscr();
{float fun;
float a,b,h,sum,x,trap;
fun=(sin(x)-log10(x)+exp(x));
int n,i,m;
return(fun);
cout<<"The given function is"<<endl;
}
cout<<"sin(x)-log10(x)+exp(x)";
cout<<"Enter the Lower Limit: ";
cin>>a;
cout<<"Enter the Upper Limit: ";
cin>>b;
cout<<"Enter the Number of Subintervals: ";
cin>>n;
h=(b-a)/n;
sum=0.0;
m=n-1;
for(i=1;i<=m;i++)
{x=a+i*h;
5
Simpson’s 1/3
cout<<"Enter Upper Limit "<<endl;
#include<iostream.h>
cin>>b;
#include<math.h>
cout<<"Enter the number of intervals : "<<endl;
#include<conio.h>
cin>>n;
float f(float(x))
d=(b-a)/n;
{
for(i=1;i<n;i++)
return (sin(x)-log10(x)+exp(x));
{
}
if((i%2)!=0)
float g(float(x))
{
{
I=I+f(a+(i*d));
return (cos(x)-(1/(x))+(x*exp(x)));
}
}
}
float h(float(x))
for(i=2;i<n-1;i++)
{
{
return (-sin(x)+(1/(x*x))+((x*x)*exp(x)));
if((i%2)==0)
}
{
int main()
J=J+f(a+(i*d));
{
}
clrscr();
}
int i;
A=(d/3)*(f(a)+(4*I)+(2*J)+f(b));
long double a,b,d,n,I=0,J=0,A,K=0,E=0;
cout<<"The Value of integral under the enterd
cout<<" Given function f(x)= sin(x)-
limits is : "<<endl;
log(x)+exp(x)"<<endl;
cout<<A<<endl;
cout<<"Enter lower limit "<<endl;
cin>>a;
6
E=-((b-a)*d*d*d*d*6/180);
cout<<"The Total Error is : "<<endl;
cout<<E<<endl;
getche();
Simpson’s 3/8
int i;
#include<iostream.h>
float a,b,d,n,I=0,J=0,A,K=0,E=0;
#include<math.h>
cout<<"Program to evaluate integral using
#include<conio.h>
Simpson's 3/8 Rule.!!";
float f(float(x)) cout<<" Given function f(x)= sin(x)-
{ log(x)+exp(x)"<<endl;
return (sin(x)-log10(x)+exp(x)); cout<<"Enter lower limit "<<endl;
} cin>>a;
float g(float(x)) cout<<"Enter Upper Limit "<<endl;
{ cin>>b;
return (cos(x)-(1/(x))+(x*exp(x))); cout<<"Enter the number of intervals : "<<endl;
} cin>>n;
float h(float(x)) d=(b-a)/n;
{ for(i=1;i<n;i++)
return (-sin(x)+(1/(x*x))+((x*x)*exp(x))); {
} I=I+f(a+(i*d));
int main() }
{ for(i=3;i<n-1;i++)
clrscr(); {
7
if((i%3)==0)
J=J+f(a+(i*d));
A=3*(d/8)*(f(a)+(3*I)-J+f(b));
cout<<"The Value of integral under the entered
limits is : "<<endl;
cout<<A<<endl;
E=-(d*d*d*d*d*6*3/80);
cout<<"The Total Error is : "<<endl;
cout<<E<<endl;
getche();