Calculator.power C# (CSharp) Method

power() public method

public power ( int n, int p ) : int
n int
p int
return int
    public int power(int n, int p)
    {
        if (n<0 || p<0) throw new Exception("n and p should be non-negative");
        return (int)Math.Pow(n,p);
    }
}

Usage Example

Beispiel #1
0
    static void Main(String[] args)
    {
        Calculator myCalculator = new  Calculator();

        int T = 4;

        string[] stringArray = new string[] { "3 5", "2 4", "-1 -2", "-1 3" };


        while (T-- > 0)
        {
            //string[] num = Console.ReadLine().Split();
            string[] num = stringArray[T].Split();
            int      n   = int.Parse(num[0]);
            int      p   = int.Parse(num[1]);
            try{
                int ans = myCalculator.power(n, p);
                Console.WriteLine(ans);
            }
            catch (Exception e) {
                Console.WriteLine(e.Message);
            }
        }

        Console.ReadKey();
    }
All Usage Examples Of Calculator::power
Calculator