c sharp

How to Use Math.Pow() Method in C#

In C#, there are many built-in classes and methods that are used for different purposes in the code and thus save the time and effort of the programmer and also reduce the complexity of the code. The “Math” classes in C# allow us to perform different mathematical functions in the code along with different methods.

This guide will explain the “Math.Pow()” method in C#.

What is Math.Pow() Method in C#

In the “Math Pow()” method “Math” is the class and “Pow()” is the method. The “Math” class allows us to perform mathematical operations in the code like logarithms and trigonometric functions. The “System” namespace contains the “Math” class. The “Pow()” method allows us to take power or two numbers. The datatype of these two numbers is double and the return type of this method is also double.

The syntax of this method is:

Math.Pow(base, power)

 

Here, “base” is the number whose power will be taken, and “power” is the number up to which the power of the base number will be taken. Both numbers are “Double” type.

See the following code to understand this method:

using System;

public class Math_power_example {
  public static void Main (string[] args) {
    double a = 2;
    double b = 4;
    Console.WriteLine("The 4th power of 2 is: " + Math.Pow(a,b));
  }
}

 

In the above code:

  • We used the “System” namespace as the “Math” class is included in the “System” namespace.
  • After that, we declare a class “Math_power_example” and create a static method “Main()”.
  • Then, we declare two variables as “a” and “b”.
  • The value of “a” is 2 which is the base and the value of “b” is 4 which is the power.
  • Lastly, we called the “Math.Pow()” method to take “2^4”.

Output

Here, we can see that the output of “2^4” is “16”.

Now, let’s see another example by changing the values of “a” and “b”:

using System;

public class Math_power_example {
  public static void Main (string[] args) {
    double a = 2.5;
    double b = 4.1;
    Console.WriteLine("The 4.1 power of 2.5 is: " + Math.Pow(a,b));
  }
}

 

In the above-discussed example:

  • We use the previously generated code.
  • Replace the values of “a” and “b” from “2” and “4” to “2.5” and “4.1” to see whether the output will show the values with decimal points or not.

Hence the output is of type double and the code is working correctly in this case.

Now, let’s add the loop in the previous example to check whether the method works correctly or not if we take the power of a number in the form of a loop:

using System;

public class Math_power_example {
  public static void Main (string[] args) {
    double a = 2;
    for (double i=1;i<=10;i++){
    Console.WriteLine("2^"+i+ " = "+ Math.Pow(a,i));
    }
  }
}

 

According to the previously provided code:

  • We have used the “System” namespace and declared a class “class Math_power_example”.
  • Then, we created a “Main()” method which is static.
  • After that, a variable “a” with value “2” is declared.
  • Next, we have a “for loop” which has a variable “i” of type double.
  • Lastly, we take the power of “a” to the range of “i”.

Output

The output shows the result of the power of “2” till “10”.

Conclusion

The “Math.Pow()” method in C# helps users to take the power of different numbers. It has two parameters of type double. One represents the “base” value and the other one represents “power”. We can use this method with loops also. In this tutorial, we have briefly described the “Math.Pow()” method in C#.

About the author

Umar Hassan

I am a Front-End Web Developer. Being a technical author, I try to learn new things and adapt with them every day. I am passionate to write about evolving software tools and technologies and make it understandable for the end-user.