Nth root calculator in C++

In this tutorial, we are going to calculate the Nth root of an integer number using a C++ program. In mathematics, many times, we need to calculate the Nth root. So, we will learn what an Nth root of a number is, and the method to calculate the Nth root of a number given by the user. We will implement the Nth root calculator using a C++ program.

The Nth root of a number in C++

Let us take an example to understand what is Nth root. Firstly, suppose a number say ‘x’. Let the Nth root of ‘x’ be ‘r’. Then, we can say that when the number ‘r’ is multiplied by itself N times, we get the number ‘x’. We can say that the Nth root of the number ‘x’ is the number ‘r’. For example –

The 2nd root of the number ‘9’ is ‘3’. Because by multiplying the number ‘3’ twice we get the number ‘9’.
Similarly, the 3rd root of the number ‘125’ is ‘5’.

User-defined function to calculate Nth root

Let’s see the user-defined function to calculate the Nth root of a number. We will take the number and the value of ‘n’ as a parameter. To find the Nth root, we have to check every number by multiplying it ‘n’ times. If the result comes out equal to the number, then it will be the Nth root of that number. The function is as follows –

int root_calc(int num,int n)
{
  int temp_num;
  for(int i=2;i<=num/2;++i)
  {
    temp_num=1;
    for(int j=1;j<=n;++j)
      temp_num*=i;
    if(temp_num==num)
      return i;
  }
  return -1;
}

The function returns the root of the number given in parameter. If the number does not have a perfect root, then we return -1 value to the calling function. In this function, every number is multiplied by itself ‘n’ times using a loop. If the root is found, the control returns to the calling function with root as the return value.

C++ program for Nth root calculator

Let’s implement the Nth root calculator using a C++ program and the user-defined function discussed above. In the main() function, we simply take the number and value of ‘N’ from the user. The program for the Nth root calculator is given below.

#include<iostream>
using namespace std;
int root_calc(int,int);
int main()
{
  int number,nth_root,res;
  cout<<"Enter A Number : ";
  cin>>number;
  cout<<"Enter Nth Root Value : ";
  cin>>nth_root;
  res=root_calc(number,nth_root);
  if(res==-1)
    cout<<"\n"<<number<<" Does Not Have Perfect Root";
  else
    cout<<"\nThe Root "<<nth_root<<" Of "<<number<<" Is : "<<root_calc(number,nth_root);
  return 0;
}
int root_calc(int num,int n)
{
  int temp_num;
  for(int i=2;i<=num/2;++i)
  {
    temp_num=1;
    for(int j=1;j<=n;++j)
      temp_num*=i;
    if(temp_num==num)
      return i;
  }
  return -1;
}

The main() function calls the root_calc() function and passes the number and ‘n’ value as parameter to the function.

The output of the program

The program can be used to calculate any root of any number. We can give any value of ‘N’ and ‘number’ to the root_calc() function. The output of the above program is –

siddharth@siddharth-Lenovo-Y520-15IKBN:~/cpp$ g++ root.cpp
siddharth@siddharth-Lenovo-Y520-15IKBN:~/cpp$ ./a.out
Enter A Number : 125
Enter Nth Root Value : 3

The Root 3 Of 125 Is : 5
siddharth@siddharth-Lenovo-Y520-15IKBN:~/cpp$

Thank you for reading this tutorial. I hope it helped you.

Also read:

How to find the cube root of a number in C++

Leave a Reply