Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
Advertisements