Find the nth Power of a given number

The power of a given number obtained by multiplying the number with itself for a specified number of times.

For a given number a, calculate the nth power where n is any number n > 0, a > 0

Example: Func(2, 5) = 5th power of 2 = 32

private double Power(double a, double n) 
{
      double mult = 1;
      for(int i = 1; i <= n; i++)
      {
          mult = mult * a;
      }
      
      return mult;
}

How it works:

mult=1, i = 1
n = 5, a = 2
mult = 1 * 2 = 2

mult=2, i=2
n=5, a=2
mult=2 * 2 = 4

and so on..


Buy Me A Coffee

Found this article helpful? Please consider supporting!

Ram
Ram

I'm a full-stack developer and a software enthusiast who likes to play around with cloud and tech stack out of curiosity. You can connect with me on Medium, Twitter or LinkedIn.

Leave a Reply

Your email address will not be published. Required fields are marked *