We use cookies to provide you with a great user experience, analyze traffic and serve targeted promotions.   Learn More   Accept

Find the nth Power of a given number


Expectation

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


Logic
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..


Loops •  Added one month ago

Sponsored Links