[go: up one dir, main page]

0% found this document useful (0 votes)
36 views1 page

Bisection Method CPP Exam

The document presents a C++ program that implements the Bisection Method to find the root of the equation 2x^2 - 38 = 0. It prompts the user for a lower limit, upper limit, and maximum error, checks if the interval brackets the root, and iteratively narrows down the interval to find the root. The program continues to run until the user decides to exit.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views1 page

Bisection Method CPP Exam

The document presents a C++ program that implements the Bisection Method to find the root of the equation 2x^2 - 38 = 0. It prompts the user for a lower limit, upper limit, and maximum error, checks if the interval brackets the root, and iteratively narrows down the interval to find the root. The program continues to run until the user decides to exit.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Bisection Method C++ Program: 2x^2 - 38 = 0

// Bisection Method to find root of 2x^2 - 38 = 0


#include <iostream>
#include <cmath>
using namespace std;

// Define the function: f(x) = 2x^2 - 38


float f(float x) {
return 2 * x * x - 38;
}

int main() {
float a, b, c, maxerr;
int i = 0;
char choice;

do {
cout << "\nEnter the lower limit, upper limit, and max error: ";
cin >> a >> b >> maxerr;

if (f(a) * f(b) >= 0) {


cout << "\nThe chosen interval does not bracket the root.\n";
} else {
i = 0;
do {
c = (a + b) / 2;
if (f(a) * f(c) < 0)
b = c;
else
a = c;
i++;
} while (fabs((b - a) / a) > maxerr);

cout << "\nRoot of the equation after " << i << " iterations = " << c <<
endl;
}

cout << "\nDo you want to continue? (y/n): ";


cin >> choice;
} while (choice == 'y' || choice == 'Y');

return 0;
}

You might also like