C Program - Cube Root of a Number
If a number is multiplied by itself two times (n*n*n), the final number will be the cube of that number and finding the cube root of a number is inverse operation of cubing the number. If x is the cube root of y, it can be expressed as below:
Alternatively, it can also be expressed as:
x3 = y
Example: Using cbrt() function of C <math.h> header file
The cbrt() function of C <math.h> header file can be used to return cube root of a number.
#include <stdio.h> #include <math.h> int main() { double x = 64; double y = 125; double x1 = cbrt(x); double y1 = cbrt(y); printf("Cube root of %.2f is %.2f\n", x, x1); printf("Cube root of %.2f is %.2f\n", y, y1); }
The above code will give the following output:
Cube root of 64.00 is 4.00 Cube root of 125.00 is 5.00
Example: Using pow() function of C <math.h> header file
The pow() function of C <math.h> header file can also be used to calculate cube root of a number.
#include <stdio.h> #include <math.h> int main() { double x = 64; double y = 125; double x1 = pow(x, 1/3.0); double y1 = pow(y, 1/3.0); printf("Cube root of %.2f is %.2f\n", x, x1); printf("Cube root of %.2f is %.2f\n", y, y1); }
The above code will give the following output:
Cube root of 64.00 is 4.00 Cube root of 125.00 is 5.00