N-th root of a number in C++



You are given the N-th root and the result of it. You need to find the number such that numberN = result.

Let's see some examples.

Input

 result = 25 N = 2

Output

 5

The 52 = 25. Hence the output in the above example is 5.

Input

result = 64 N = 3

Output

4 

The 43 = 64. Hence the output in the above example is 4.

Algorithm

Implementation

Following is the implementation of the above algorithm in C++

#include <bits/stdc++.h> using namespace std; int getNthRoot(int result, int n) {    int i = 1;    while (true) {       if (pow(i, n) == result) {          return i;       }       i += 1;    } } int main() {    int result = 64, N = 6;    cout << getNthRoot(result, N) << endl;    return 0; }

Output

If you run the above code, then you will get the following result.

2
Updated on: 2021-10-22T07:38:31+05:30

908 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements