Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to calculate the power exponent value using C#?
To calculate the power exponent value in C#, use the Math.Pow() method from the System namespace. This method raises a number to a specified power and returns the result as a double.
Syntax
Following is the syntax for the Math.Pow() method −
public static double Pow(double x, double y)
Parameters
-
x ? The base number (of type
double). -
y ? The exponent (of type
double).
Return Value
Returns a double value representing x raised to the power of y (xy).
Using Math.Pow() with Integers
Example
using System;
class Program {
static void Main() {
double n, p;
n = 7;
p = 3;
Console.WriteLine("Base= " + n);
Console.WriteLine("Exponent= " + p);
double res = Math.Pow(n, p);
Console.WriteLine("Result= {0}", res);
}
}
The output of the above code is −
Base= 7 Exponent= 3 Result= 343
Using Math.Pow() with Decimal Exponents
Example
using System;
class Program {
static void Main() {
double number = 16;
double power = 0.5; // Square root
double result = Math.Pow(number, power);
Console.WriteLine("{0} raised to the power of {1} = {2}", number, power, result);
// Another example with negative exponent
double negPowerResult = Math.Pow(2, -3);
Console.WriteLine("2 raised to the power of -3 = {0}", negPowerResult);
}
}
The output of the above code is −
16 raised to the power of 0.5 = 4 2 raised to the power of -3 = 0.125
Multiple Power Calculations
Example
using System;
class Program {
static void Main() {
double[] bases = {2, 3, 5};
double[] exponents = {2, 3, 2};
Console.WriteLine("Power Calculations:");
Console.WriteLine("Base\tExponent\tResult");
Console.WriteLine("----\t--------\t------");
for(int i = 0; i
The output of the above code is −
Power Calculations:
Base Exponent Result
---- -------- ------
2 2 4
3 3 27
5 2 25
Conclusion
The Math.Pow() method in C# is the standard way to calculate power exponent values. It accepts two double parameters and returns the base raised to the specified power, handling both positive and negative exponents as well as fractional powers for operations like square roots.
