12+ CPP Cube Root Finder Program Examples

This post contains a total of 12+ Hand-Picked C++ Cube Root Finder Program Examples with Source Code. All these Cube root programs are made using C++ Programming language.

You can use the source code of these examples with credits to the original owner.

Related Posts

C++ Cube Root Finder Programs

1. By Szaky

Made by Szaky. A simple C++ Program to find the cube root of a given number. Source

Input: 10
Output: 2.15444
#include <iostream>
using namespace std;

int main()
{
    double number;
    cin >> number;
    cout << "Input: " << number << endl;
    cout << "Output: ";
    // if number is negative
    if (number < 0) {
        number*=-1;     //change 
        cout << "-";
    }
    // trivial roots
    if (number == 1 || number == 0)
    {
        cout << number;
        return 0;
    }
    
    // interval halving
    // initial upper limit 
    double up = number;     
    if (number < 1) up = 1; 
    // initial lower limit
    double low = 0;
    // mid point (root)
    double r = (up + low) / 2;  
    // stopping condition (abs(error))<0.0001
    while (number - (r*r*r) > 0.0001 || number - (r*r*r) < -0.0001)
    {
        // movement of the lower or upper limit
        if ((number - (low*low*low)) * (number - (r*r*r)) < 0 ) up = r;
        else low = r;
        // new mid point (root)
        r = (up + low) / 2;
    }

    cout << r;

    return 0;
}

2. By Disvolvigo

Made by Disvolvigo. C++ Cube root program without using any Libraries. Source

12
4.66667
3.29478
2.565
2.31797
2.28978
2.28943
2.28943
2.28943
2.28943 <- Result!!
#include <iostream>
using namespace std;

double cubeRoot(double n);

int main()
{
    double n;
    cin >> n;
    cout << cubeRoot(n) << " <- Result!!" << endl;
    return 0;
}

double cubeRoot(double n)
{
    //with Newton's mothod.
    double x = 1, e = 1;
    while((e < 0 ? -e : e) > 10e-14)
    {
        e = (x*x*x - n) / (3*x*x);
        x = x - e;
        cout << x << endl;
    }
    return x;
}

3. By Ankith R V

Made by Ankith R V. Source

Input - 5
Output - 1.7099
#include <iostream>

using namespace std;
int main() {
int n, m, D=0  ;
double  d =0;
cin>>n;

for(float i=0;i<n;)
{
    m=(i*i)*i;
    if(m == n )
    {
    D=i*100000;
    d=D%10;
    d/=100000;
    cout<<"Input - "<<n<<endl;
    cout<<"Output - "<<i-d;
      
    break;
    }
     
     i=i+0.00001;
}


    return 0;
}

4. By NC

Made by NC. Simple C++ program to Find Cube Root ( Whole Range ). Source

Enter the number: 15
Cube Root of 15 is 2.46621
#include <iostream>
using namespace std;

int main() {
    cout << "Enter the number: ";
    float n;
    cin >> n;
    cout << n << endl;
    
    float x0 = n/2.0, x, d; // random initial guess
    
    int count = 1;
    
    do{
        x = (1.0/3.0)*((2.0*x0) + (n/(x0*x0)));
        //cout << "Iteration No.: " << count << " Possible Root: " << x << endl ;
        ++ count ;
        d = x0;
        x0 = x;
    }while ((d > x && (d-x) > 0.0001) || (d < x && (x-d) > 0.0001));
    
    /*
    f(x) = x*x*x - n
    f'(x) = 3*x*x
    x(n+1) = x(n) - (f(x(n))/f'(x(n))) 
            = (1/3)*(2*x + (n/x^2))
    |x(n+1) - x(n)| <= 0.0001
    */
    
    cout << "Cube Root of " << n << " is " << x;
    
    return 0;
}

5. By Naveen Maurya

Made by Naveen Maurya. Cube root and square root finder program without using libraries. Source

Enter a positive number to find out its cube root: 10
The square root of 10 is 3.16228
The cube root of 10 is 2.15443
#include <iostream>
using namespace std;

double precision = 0.0000001;
double increment = 0.0000001;

double squareRoot(double num) {
    for(double i = 0; i < num; i+=increment) {
        if(num - i*i < precision) {
            return i;
        }
    }
}

double cubeRoot(double num) {
    for(double i = 0; i < num; i+=increment) {
        if(num - i*i*i < precision) {
            return i;
        }
    }
}

int main() {
    double num = 10;//Default value
    cout << "Enter a positive number to find out its cube root: ";
    cin >> num;
    cout << "\nThe square root of " << num << " is " << squareRoot(num);
    cout << "\nThe cube root of " << num << " is " << cubeRoot(num);
    return 0;
}

6. By Bilal Ahmed

Made by Bilal Ahmed. Source

Enter number : square root of 6 is 2.44949

cube root of 6 is 1.81712
#include <iostream>
#include <cmath>

using namespace std;

int main()
{

    //variable declaration
    int number;

    //take input for number from user
    cout << "Enter number : ";
    cin >> number;

    //use of sqrt function to find square root
    cout << "square root of " << number << " is " << sqrt(number);

    cout << "\n\n";

    //use of cbrt function to find cube root
    cout << "cube root of " << number << " is " << cbrt(number);

    cout << "\n";

    return 0;
}

7. By Mohammad Amir Aqeel

Made by Mohammad Amir Aqeel. Program without library function to find cube root. Source

Enter A Number To Find Its Cube : 20
Cube of 20 is : 2.7144
#include <iostream>

using namespace std;

int main()
{
    double a,b,c,d,e,i,j=1,res=0,f,k,t=10,flag;
    cout<<"Enter A Number To Find Its Cube : ";
    cin>>a;
    if(a<0){
        a+=(-2*a);
        flag=1;
    }
    for(i=0;i<=a;i++){
        if((i*i*i)==a){
            b=i;
            j=5;
        }
        if((i*i*i)>a){
            b=i-1;
            break;
        }
    }
    c=a-(b*b*b);
    c*=1000;
    res+=b;
    while(j!=5){
        for(k=1;k<c;k++){
            e=(300*b*b)+(30*b*k)+(k*k);
            d=e*k;
            if(d>c){
                k=k-1;
                break;
            }
        }
        e=(300*b*b)+(30*b*k)+(k*k);
        d=e*k;
        f=k/t;
        res+=f;
        c=(c-d)*1000;
        b=res*t;
        t*=10;
        j++;
    }
    if(flag==1){
        res*=(-1);
    }
    cout<<"Cube of "<<a<<" is : "<<res;
}

8. By Logan Potts

Made by Logan Potts. Source

2
1.22743
#include <vector>
#include <iostream>
using namespace std;

int main(){
	int c;
	cin >> c;
	vector <int> low_est;
	for(int i=0;i*i*i<c;i++){
		low_est.push_back(i);
	}
	int a;
	a = low_est.back();
	low_est.clear();
	double b = a+1;
	double x = (a+b)/2;
	double y = x;
	for(int i=1;i<1000000;i++){
		if(x*x*x>c){
			b=x;
			x = (x+a)/2;
			y=x;
		} else {
			a = x;
			x = (x+b)/2;
			y=x;
		}
	}
	cout << x;
	return 0;
}

9. By Paolo De Nictolis

Made by Paolo De Nictolis. Source

Enter a number to calculate its cube root: 5
Cube root of 5 is 1.70998

#include <iostream>
#include <string>
using namespace std;

int main() {
    string strNum;
    double num;
    double croot = 0.0;
    cout << "Enter a number to calculate its cube root:" << endl;
    cin >> strNum;
    num = stod(strNum);
    while(croot * croot * croot < num){
        croot++;
    }
    croot -= 0.5;
    while(abs((croot * croot * croot) - num)>0.00001){
        croot = (2*croot+num/(croot*croot))/3; // Newton-Raphson formula
    }
    cout << "Cube root of " << strNum << " is " << croot;
    return 0;
}

10. By Madhav

Made by Madhav. C++ program to find cube root. Source

Your input : 10 .

Approximations :
1.  4
2.  2.875
3.  2.31994
4.  2.16596
5.  2.1545
6.  2.15443
#include <iostream>
using namespace std;

int main() {

//Taking Input
string number;
double y,num; 
int d=1,dig;

cin>>number;  
cout<<"Your input : "<<number<<" .\n\n";

dig=number.length()-1;
num=stod(number);

//To stop program if input is 0 or 1
if(num==0||num==1||num==-1)
{
    cout<<"=>Therefore "<<num<<" is the cube root of "<<num<<" ."; return 0;
}


//To help in guessing the approx value
for(int j=1;j<=dig;j++)
   {        d*=10;    }
       y=num/d;
 cout<<"Approximations :\n";


/*  By Newton Raphson Method;
    
i.e f(x) = x*x*x - num
    f'(x) = 3*x*x
    
    x a+1 = x a - (f(x) / f'(x))
*/
for(int i=1;;i++)
{   
  y=y-(y*y*y-num)/(3*y*y);  
  cout<<i<<".  "<<y<<endl  ;
  if(y*y*y<=num)
  {    
//Printing results   
cout<<"\n=>Therefore; "<<y<<" is the cube root of "<<num<<" .";
      return 0;
  }
}
      return 0;
}

11. By K Martin

Made by K Martin. Cube root finder program using loops. Source

12
2.2895
#include <iostream>
using namespace std;
float a;
float c=0;
int x=0;
int y=0;
int z=0;
int q=0;
int w=0;
int main() {
cin>>a;
while(x<1){
    c=c+1;
    if(a==c*c*c){
        x=2;
        cout<<c;
    }
    if(a<c*c*c){
    
         c=c-1;
        
        while(y<1){
            c=c+0.1;
            if(c*c*c==a){
                y=2;
                x=2;
                cout<<c;
            }
            
     if(a<c*c*c){
    
         c=c-0.1;
        
        while(z<1){
            c=c+0.01;
            if(a==c*c*c){
                 x=2;
                 y=2;
                z=2;
                cout<<c;
            }
            if(a<c*c*c){
    
         c=c-0.01;
        
        while(q<1){
            c=c+0.001;
            if(a==c*c*c){
                x=2;
                y=2;
                z=2;
                q=2;
                cout<<c;
            }
            if(a<c*c*c){
                c=c-0.001;
                while(w<1){
                    c=c+0.0001;
                    if(a==c*c*c){
                    x=2;
                    y=2;
                    z=2;
                    q=2;
                    w=2;
                    
                    cout<<c;
                        
                    }
                    if(c*c*c>a){
                        x=2;
                        y=2;
                        z=2;
                        q=2;
                        w=2;
                    
                        cout<<c;
                    }
                }
            }
            
            
        }
    }
}

    
    }
    }
    }
    }
    
    return 0;
    
    }

12. By Navneet Sharma

Made by Navneet Sharma. Source

Enter any number
Cube of 5 is: 1.70998
#include <iostream>
#include<cmath>
using namespace std;

int main() {
float num, ans;
 cout<<"Enter any number"; cin>>num;
 ans=pow(num,1.0/3.0);
 //ans++;
 cout<<"\n\Cube of "<<num<<" is: "<<ans;
    return 0;
}

13. By Nick

Made by Nick. Input a number (can be a decimal number too) and the program will try to find it’s cube root by approaching it’s value. You can manipulate the accuracy of the output number by changing the “y” variable. The higher the value the more accurate the output will be but it will also be more likely to exceed the time limit so if it doesn’t work lower the accuracy. Excluded negative numbers and 0. Source

15
13.824 < 15 < 15.625
14.8869 < 15 < 15.0692
14.9961 < 15 < 15.0144
14.9998 < 15 < 15.0016
14.9999 < 15 < 15.0001
The cube root of 15 is approximately 2.46621.
#include <iostream>
using namespace std;

int main() {
    float num, i, x, y;
    bool isTrue = false;
    cin >> num;
    i = 1000;
    x = i / 10;
    y = 5; //This value is the accuracy
    if (num <= 0) {
        cout << "number must be higher than 0";
        return 0;
    }
    while (i*i*i > num) {
        i /= 10;
        x /= 10;
    }
    while (i*i*i*100 < num) {
        i *= 10;
        x *= 10;
    }
    while (y > 0) {
            while (i*i*i < num) {
                i += x;
            }
        if (i*i*i == num) {
            isTrue = true;
            break;
        }
        i -= x;
        if (i*i*i == num) {
            isTrue = true;
            break;
        }
        cout << i*i*i << " < " << num << " < " << (i+x)*(i+x)*(i+x) << "\n";
        x /= 10;
        y--;
    }
    if (isTrue == true) {
        cout << "The cube root of " << num << " is " << i << ".";
    }
    if (isTrue == false) {
        cout << "The cube root of " << num << " is approximately " << i << ".";
    }
    return 0;
}