Math.Pow() Method in C#

The Math.Pow() method in C# is used to compute a number raised to the power of another number. It returns the result as a double value and can handle both positive and negative bases and exponents.

Syntax

Following is the syntax −

public static double Pow(double val1, double val2)

Parameters

  • val1 − A double-precision floating-point number to be raised to a power (the base).

  • val2 − A double-precision floating-point number that specifies the power (the exponent).

Return Value

Returns a double representing val1 raised to the power of val2. Special cases include:

  • Any number raised to the power of 0 equals 1

  • 0 raised to any positive power equals 0

  • Negative base with fractional exponent returns NaN

Using Math.Pow() with Basic Operations

Example

using System;

public class Demo {
   public static void Main() {
      double res;
      res = Math.Pow(5, 0);
      Console.WriteLine("Math.Pow(5,0) = " + res);
      res = Math.Pow(0, 5);
      Console.WriteLine("Math.Pow(0,5) = " + res);
      res = Math.Pow(3, 2);
      Console.WriteLine("Math.Pow(3,2) = " + res);
      res = Math.Pow(2, -3);
      Console.WriteLine("Math.Pow(2,-3) = " + res);
   }
}

The output of the above code is −

Math.Pow(5,0) = 1
Math.Pow(0,5) = 0
Math.Pow(3,2) = 9
Math.Pow(2,-3) = 0.125

Using Math.Pow() with Fractional Exponents

Example

using System;

public class Demo {
   public static void Main() {
      double res;
      res = Math.Pow(16, 0.5);
      Console.WriteLine("Math.Pow(16,0.5) = " + res + " (Square root)");
      res = Math.Pow(8, 1.0/3.0);
      Console.WriteLine("Math.Pow(8,1/3) = " + res + " (Cube root)");
      res = Math.Pow(25, 0.5);
      Console.WriteLine("Math.Pow(25,0.5) = " + res);
      res = Math.Pow(27, 1.0/3.0);
      Console.WriteLine("Math.Pow(27,1/3) = " + res);
   }
}

The output of the above code is −

Math.Pow(16,0.5) = 4 (Square root)
Math.Pow(8,1/3) = 2 (Cube root)
Math.Pow(25,0.5) = 5
Math.Pow(27,1/3) = 3

Using Math.Pow() with Special Cases

Example

using System;

public class Demo {
   public static void Main() {
      double res;
      res = Math.Pow(-2, 3);
      Console.WriteLine("Math.Pow(-2,3) = " + res);
      res = Math.Pow(-8, 1.0/3.0);
      Console.WriteLine("Math.Pow(-8,1/3) = " + res);
      res = Math.Pow(0, 0);
      Console.WriteLine("Math.Pow(0,0) = " + res);
      res = Math.Pow(Double.PositiveInfinity, 2);
      Console.WriteLine("Math.Pow(PositiveInfinity,2) = " + res);
   }
}

The output of the above code is −

Math.Pow(-2,3) = -8
Math.Pow(-8,1/3) = NaN
Math.Pow(0,0) = 1
Math.Pow(PositiveInfinity,2) = Infinity

Common Use Cases

Operation Math.Pow() Usage Result
Square root Math.Pow(x, 0.5) ?x
Cube root Math.Pow(x, 1.0/3.0) ?x
Square Math.Pow(x, 2)
Reciprocal Math.Pow(x, -1) 1/x

Conclusion

The Math.Pow() method is a versatile function for performing exponential calculations in C#. It handles various mathematical operations including powers, roots, and special cases, making it essential for mathematical computations and scientific applications.

Updated on: 2026-03-17T07:04:35+05:30

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements