Cube Root of Number in C++


To find cube root of any number we need to find 0.3 power of any number. For example if you need to find cube root of 27 then calculate 0.3 power of 27, result is 3. And one another method for this program is use cbrt() function it is pre-defined in math.h header file.

Cube Root program in C++

#include<iostream.h>
#include<conio.h>
#include<math.h>

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

Output

Enter any Number: 27
Cube of 27 is: 3

Explanation of Program

  • Here we receive any number from keyboard and after that we find 1.0/3.0 power of number because square root of any number means power of 1/3.
  • pow() is a predefined function in math.h header file, it is used for calculate power of any number.

C++ program to find Cube Root

#include<iostream.h>
#include<conio.h>
#include<math.h>

void main() 
{ 
int num,ans; 
clrscr(); 
cout<<"Enter any Number: "; 
cin>>num; 
ans=cbrt(num);;
cout<<"\n Cube Root of "<<num<<" is: "<<ans; 
getch(); 
} 

Output

Enter any Number: 27
Cube Root of 27 is: 3

Explanation of Code

cbrt() is a predefined function in math.h header file, it is used for calculate cube root of any number.

Cube Root of number using user defined function

Cube Root program in C++

#include<iostream.h>
#include<conio.h>

int cube(int); 
void main() 
{ 
int num; 
clrscr(); 
cout<<"Enter any number"; 
cin>>num; 
cout<<"\n\Cube of "<<num<<" is: "<<cube(num); 
getch(); 
} 
int cube(int num) 
{ 
int ans; 
ans=pow(num, 0.3);
ans++; 
return(ans); 
getch(); 
}

Output

Enter any Number: 64
Cube of 64 is: 4

Explanation of Program

  • Here we receive any number from keyboard and after that we multiply number by 0.33333 because square root of any number means power of 1/3 and 1/3=0.33333.
  • pow() is a predefined function in math.h header file, it is used for calculate power of any number.

0 comments: