Bisection Method Program in C Language

Programming Knight
2 min readAug 1, 2022

The bisection method is a technique used in C programming language to divide a number into two parts.

The bisection method is a technique used in C programming language to divide a number into two parts. It can be implemented as follows:

int x = 10;

int y = x / 2; int z = x — y;

This code will give the result of 5.

Algorithm for Bisection Method Program in C

The algorithm for Bisection Method Program in C is an important tool used to find the midpoint of a given set of data.

The bisection method program is a divide and conquer algorithm that can be used to find the midpoint of a set of data. It can also be used to find the median if the data consists of two numbers. The bisection method program divides a given set into two subsets and then uses recursion to calculate the midpoint for each subset. .When applied to an array of data, the bisection method program uses recursion to calculate the midpoint and return it.

If the array is a two-dimensional array, then both halves are returned as independent arrays. This process continues until only one value remains when all other values have been divided into subsets.

The bisection algorithm finds the largest set of values in a given array by taking the midpoint between the largest and smallest element in the set.In order to run the bisection method, a new array is created with the smallest value as its first element.

Program on Bisection Method in C Language

Let’s the program now.

#include<stdio.h>
#include<math.h>

double F( double x) //Function definition
{
//This return the value of the Function
return (10 — pow(x,2));
}

int main()
{
printf(“Studytonight — Best place to learn\n”);

printf(“This program illustrates the bisection method in C:\n”);
printf(“ f(x) = 10 — x²”);
double x0,x1;

printf(“\nEnter the first approximation to the root : “);
scanf(“%lf”,&x0);

printf(“Enter the second approximation to the root : “);
scanf(“%lf”,&x1);

int iter;
printf(“Enter the number of iteration you want to perform : “);
scanf(“%d”,&iter);

int ctr = 1;
double l1 = x0;
double l2 = x1;
double r,f1,f2,f3;

//We check if the initial approximations are the root or not
if(F(l1)==0)// it is a root
r = l1;
else if(F(l2)==0)
r = l2;
else //If the above two values are not the roots of the given function
{
while(ctr <= iter) //Since, ctr is initialized to 1
{
//This is an implementation of the algorithm mentioned above
f1 = F(l1);
r = (l1+l2)/2.0; //Returns a double value
f2 = F(r);
f3 = F(l2);

if(f2 == 0)
{
r = f2;
break; //Execution moves out of the while loop to the statement just after it
}
printf(“The root after %d iteration is %lf\n”,ctr,r);

if(f1*f2 < 0)//Both are of opposite sign
l2 = r;
else if(f2*f3 < 0)
l1 = r;
ctr++;
}
}

printf(“The approximation to the root is %lf\n”,r); //Gives the final value after mentioned iteration
printf(“Coding is Fun !”);
return 0;
}

--

--