C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Parameters: The first parameter indicates the number you are trying to base the result on. The second parameter is the exponent itself.
Tip: If you are trying to square two, you will use 2, 2 as the parameters. Visual Studio provides hints on the use of parameters.
Here: The program first uses the Math.Pow method in the System namespace by calling it with common small integer parameters.
Finally: The program also cubes those small integers. The Math.Pow method returns a double type number.
C# program that uses Math.Pow method
using System;
class Program
{
static void Main()
{
//
// Compute the squares of the first parameters to Math.Pow.
//
double value1 = Math.Pow(2, 2);
double value2 = Math.Pow(3, 2);
double value3 = Math.Pow(4, 2);
double value4 = Math.Pow(5, 2);
//
// Compute the cubes of these values.
//
double value5 = Math.Pow(2, 3);
double value6 = Math.Pow(3, 3);
double value7 = Math.Pow(4, 3);
double value8 = Math.Pow(5, 3);
//
// Check some edge cases with the smallest, biggest,
// ... and special values.
//
double value9 = Math.Pow(double.MinValue, double.MaxValue);
double value10 = Math.Pow(double.MinValue, 0);
double value11 = Math.Pow(double.NaN, 2);
double value12 = Math.Pow(double.PositiveInfinity, 2);
double value13 = Math.Pow(double.NegativeInfinity, 2);
//
// Compute fractional powers.
//
double value14 = Math.Pow(2, 2.1);
double value15 = Math.Pow(Math.E, 2);
double value16 = Math.Pow(Math.PI, 1);
//
// Write results to the screen.
//
Console.WriteLine(value1);
Console.WriteLine(value2);
Console.WriteLine(value3);
Console.WriteLine(value4);
Console.WriteLine(value5);
Console.WriteLine(value6);
Console.WriteLine(value7);
Console.WriteLine(value8);
Console.WriteLine(value9);
Console.WriteLine(value10);
Console.WriteLine(value11);
Console.WriteLine(value12);
Console.WriteLine(value13);
Console.WriteLine(value14);
Console.WriteLine(value15);
Console.WriteLine(value16);
}
}
Output
4 Two squared.
9 Three squared.
16 Four squared.
25 Five squared.
8 Two cubed (2 * 2 * 2).
27 Three cubed (3 * 3 * 3).
64 Four cubed (4 * 4 * 4).
125 Five cubed (5 * 5 * 5).
Infinity
1
NaN
Infinity
Infinity
4.28709385014517
7.38905609893065
3.14159265358979
Note: The Math.Pow method is implemented in code external to the managed code shipped as part of the .NET Framework.