# N-th root of a number in C++

C++Server Side ProgrammingProgramming

#### C in Depth: The Complete C Programming Guide for Beginners

45 Lectures 4.5 hours

#### Practical C++: Learn C++ Basics Step by Step

Most Popular

50 Lectures 4.5 hours

#### Master C and Embedded C Programming- Learn as you go

66 Lectures 5.5 hours

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.

## 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 22-Oct-2021 07:38:31