C# Examples

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:

cube root

Alternatively, it can also be expressed as:

x3 = y

Method 1: Using Cbrt() method of C# Math Class

The Cbrt() method of C# Math class can be used to return cube root of a given number.

using System;

class MyProgram {
  static void Main(string[] args) {
    double x = 8;
    double y = 27;

    //Cbrt() takes double datatype as argument
    double x1 = Math.Cbrt(x);
    double y1 = Math.Cbrt(y);
    Console.WriteLine("Cube root of " + x + ": " + x1);
    Console.WriteLine("Cube root of " + y + ": " + y1);
  }
}

The above code will give the following output:

Cube root of 8: 2
Cube root of 27: 3

Method 2: Using Pow() method of C# Math Class

The pow() method of C# Math class can be used to calculate cube root of a number.

using System;

class MyProgram {
  static void Main(string[] args) {
  double x = 64;
  double y = 125;

  //Pow() takes double datatype as argument
  double x1 = Math.Pow(x, 1/3.0);
  double y1 = Math.Pow(y, 1/3.0);
  Console.WriteLine("Cube root of " + x + ": " + x1);
  Console.WriteLine("Cube root of " + y + ": " + y1);
  }
}

The above code will give the following output:

Cube root of 64: 4
Cube root of 125: 5