Search

# Bisection Method in C

Updated: Aug 26

#include<stdio.h>

#include<stdlib.h>

#include<math.h>

#define f(x) (x*x*x - x - 1)

int main() {

float x0,a,b;

int k;

float e = .001;

do{

printf("please enter the value of a and b : ");

scanf("%f%f",&a,&b);

if (f(a)==0) {

printf("\nRoot of the equation is %f",a);

break;

}

else if(f(b)==0) {

printf("\nRoot of the equation is %f",b);

break;

}

}while(f(a) * f(b) > 0);

do {

k++;

x0 = (a * f(b)- b * f(a))/(f(b)-f(a));

if(f(x0)*f(a)==0) {

break;

}

else if(f(x0)*f(a)<0) {

b=x0;

}

else if(f(x0)*f(a)>0) {

a=x0;

}

printf("\nupdated value : a = %f and b = %f",a,b);

}while(fabs(f(x0))>e);

printf("\nThe root of the equation is %f and the total number of iterations is %d",x0,k);

}

please enter the value of a and b : 1 2

updated value : a = 1.166667 and b = 2.000000

updated value : a = 1.253112 and b = 2.000000

updated value : a = 1.293437 and b = 2.000000

updated value : a = 1.311281 and b = 2.000000

updated value : a = 1.318989 and b = 2.000000

updated value : a = 1.322283 and b = 2.000000

updated value : a = 1.323684 and b = 2.000000

updated value : a = 1.324280 and b = 2.000000

updated value : a = 1.324532 and b = 2.000000

The root of the equation is 1.324532 and the total number of iterations is 9

--------------------------------

Process exited after 6.202 seconds with return value 77

Press any key to continue . . .

30 views

### Recent Posts

See All

#### Matrix Input using Array in C

#include<stdio.h> int main() { int a; int i, j, m, n; printf("please enter the row and column number: "); scanf("%d %d",&m,&n); for(i=0;i<m;i++) { for(j=0;j<n;j++) { printf("\n Please enter a(

#### Bubble Sort in C

//bubble sort #include<stdio.h> #include<conio.h> int main() { int n,i,j,temp; int arr[n]; printf("please enter how many number do you want to put?"); scanf("%d",&n); for(i=0;i<n;i++) { printf("\nente

#### Addition Of Two Numbers in C

#include<stdio.h> #include<conio.h> void main() { int a; int b; int c; printf("Please enter the first Number: "); scanf("%d",&a); printf("Please enter the second Number: "); scanf("%d",&b); c=a+b; pri