-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbisection.c
More file actions
48 lines (48 loc) · 766 Bytes
/
Copy pathbisection.c
File metadata and controls
48 lines (48 loc) · 766 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <stdio.h>
float f(float x)
{
return (x * x) - 5;
}
int main()
{
float a, b;
int flag = 0;
do
{
printf("\nEnter the values of a and b: ");
scanf("%f%f", &a, &b);
if ((f(a) * f(b)) < 0)
{
flag = 1;
printf("Two roots lie b/w %f and %f \n", a, b);
}
else
{
printf("Invalid Input. Try Again!!! \n");
}
} while (flag == 0);
int itr;
printf("\nEnter the no. of iteration: ");
scanf("%d", &itr);
int count = 1;
while (count <= itr)
{
float x2 = (a + b) / 2;
float ans = f(x2);
printf("\nx%d = %f", count, x2);
printf("\t\tf(x%d) = %f", count, ans);
if (f(a) * f(x2) > 0)
{
a = x2;
// -ve replace with -eve
// if x2 is -ve and a also -ve
// then -ve * -ve -> +ve > 0 so a = x2;
}
else
{
b = x2;
}
count++;
}
return 0;
}