Ever wondered, how can we evaluate an exponent in C#?

In this blog post, we will discuss how we add an exponent in C#.

After reading this post, you will be able to do exponent in C#.

 

 

C# Exponent Using Math.Pow() Method - Code Example

In this code example, we will use the Math.Pow() method to evaluate an exponent in C#.

Let's see the code example first and then we will explain it.

// Calculate exponent in C# using Math.Pow() var exponentValue = Math.Pow(2, 3); // Display The Exponent Console.WriteLine(exponentValue);

 

Result:

8

 

In the above code example, we calculate 2 raised to the power of 3.

In C#, we calculate the exponent using Math.Pow() method.

The first parameter of the Math.Pow method takes the number that needs to be raised.

The second parameter of the Math.Pow method takes the number that specifies a power.

so eventually Math.Pow(2,3) would mean 23 and the result would be 8 in both cases.

 

 

Math.Pow() Method In C# - Explained

The Math.Pow() method in C# is used to return a specified number raised to the specified power.

 

Syntax:

public static double Pow (double x, double y);

 

Parameters:

The first parameter (x) of the Math.Pow function is a double-precision floating-point number to be raised to a power.

The second parameter (y) of the Math.Pow function is a double-precision floating-point number that specifies a power.

 

Returns:

The number x raised to the power y.

 

 

Code Examples - Exponent Of A Number Using Math.Pow

Let's display the exponent of the first ten numbers (1-10) raised to the power 2.

for (int i = 1; i < 11; i++) { var exponentValue = Math.Pow(i, 2); // Display The Exponent Console.WriteLine($"{i} raised to the power 2 = {exponentValue}" ); }

 

Result:

1 raised to the power 2 = 1 2 raised to the power 2 = 4 3 raised to the power 2 = 9 4 raised to the power 2 = 16 5 raised to the power 2 = 25 6 raised to the power 2 = 36 7 raised to the power 2 = 49 8 raised to the power 2 = 64 9 raised to the power 2 = 81 10 raised to the power 2 = 100