From 875560927d3380a7ef5ea7e37a2af6b3396d286a Mon Sep 17 00:00:00 2001 From: Kapil Tekwani Date: Fri, 2 Oct 2020 13:08:38 +0530 Subject: [PATCH] Add files via upload Program to find the nth root of a number. --- Root.cpp | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 Root.cpp diff --git a/Root.cpp b/Root.cpp new file mode 100644 index 0000000..8c35dc7 --- /dev/null +++ b/Root.cpp @@ -0,0 +1,60 @@ +#include +#include +#include +using namespace std; + +// Function to find the nth root of a number +void findNthRoot(double x, int n) +{ + +// Initialize boundary values +double low, high; +if (x >= 0 and x <= 1) +{ + low = x; + high = 1; +} +else +{ + low = 1; + high = x; +} + +// Used for taking approximations of the answer + +double epsilon = 0.00000001; + +// Do binary search +double guess = (low + high) / 2; +while (abs((pow(guess, n)) - x) >= epsilon) +{ + if (pow(guess, n) > x) + { + high = guess ; + } + else + { + low = guess ; + } + guess = (low + high) / 2; +} + +cout +cout << fixed << setprecision(16) + << guess; +} + +// Main code +int main() +{ + double x; + int n; + cout << "Enter the number whose root has to be find: "; + cin>>x; + cout << "\n"; + cout << "Enter which root has to be find: "; + cin >> n; + findNthRoot(x, n) ; + + getch(); +}