C++ Program to Find Cube Root of a Number

In this post, we will learn how to find the cube root of a number using C++ Programming language.

The cube root of a number is the factor that we multiplied by itself three times to get that number.

Finding the cube root of a number is the opposite of cubing a number.

We will find the cube root of a number using the following approaches:

  1. Using Pow() Function
  2. Using cbrt() Function

So, without further ado, let’s begin this tutorial.

C++ Program to Find Cube Root of a Number

// C++ Program to Find Cube Root of a Number
#include <iostream>
#include <cmath>
using namespace std;

int main(){
    int n, cubeRoot;
    
    // Asking for input
    cout << "Enter a number to find cube root: ";
    cin >> n;
    
    // Calculating cube root
    cubeRoot = pow(n, 1.0/3.0);
    
    // Displaying output
    cout << "Cube root of " << n << " is: " << cubeRoot;
    return 0;
}

Output

Enter a number to find cube root: 8
Cube root of 8 is: 2

How Does This Program Work ?

    int n, cubeRoot;

In this program, we have declared two integer data type variables named n and cubeRoot.

    // Asking for input
    cout << "Enter a number to find cube root: ";
    cin >> n;

The user is asked to enter an integer to find it’s cube root. This number gets stored in the n named variable.

    // Calculating cube root
    cubeRoot = pow(n, 1.0/3.0);

We used the pow() function to find the cube root of the entered number. The pow(base, exponent) function returns the value of a base raised to the power of another exponent. 

pow(base, exponent) = base^ exponent

So, in this case we get: n which is the cube root of any number.

    // Displaying output
    cout << "Cube root of " << n << " is: " << cubeRoot;

The cube root calculated above is displayed on the screen using the cout statement.  

C++ Program to Find Cube Root of a Number Using cbrt() Function

// C++ Program to Find Cube Root of a Number Using cbrt() Function
#include <iostream>
#include <cmath>
using namespace std;

int main(){
    int n, result;
    
    // Asking for input
    cout << "Enter an integer: ";
    cin >> n;
    
    // Finding cube root using cbrt() function
    result = cbrt(n);
    
    // Displaying output
    cout << "Cube root of " << n << " is: " << result;
    return 0;
}

Output

Enter an integer: 27
Cube root of 27 is: 3

Conclusion

I hope after going through this post, you understand how to find the cube root of a number using C++ Programming language.

If you have any doubt regarding the program, then contact us in the comment section. We will be delighted to assist you.

Also Read:

Leave a Comment

Your email address will not be published. Required fields are marked *