Sort by

recency

|

618 Discussions

|

  • + 0 comments

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

  • + 1 comment

    In C#

    public class Calculator
    {
        public int power(int num, int pow)
        {
            if(num < 0 || pow < 0)
                throw new Exception("n and p should be non-negative");
                
            return (int)Math.Pow(num, pow);
        }
    }
    
  • + 0 comments
    python 3
    
    class Calculator():
    
        def power(self,n,p):
            if n < 0 or p < 0:
               raise ValueError('n and p should be non-negative') 
            return n ** p
    
  • + 0 comments

    My 2 cents in Java:

    class Calculator {
    
        public int power(int n, int p) {
            if (n < 0 || p < 0) {
                throw new RuntimeException("n and p should be non-negative");
            }
            return (int) Math.pow(n, p);
        }
    
    }
    
  • + 1 comment

    python3

    import math

    class Calculator: def init(self): pass

    def power(self,num1,num2):
        if num1 >= 0 and num2 >= 0 :
            return num1**num2
        elif num1 >= 0 and num2 <= 0:
            return "n and p should be non-negative"  
        elif num1 <= 0 and num2 >= 0:
            return "n and p should be non-negative"      
        else:
            return "n and p should be non-negative"  
    

    myCalculator=Calculator() T=int(input()) for i in range(T): n,p = map(int, input().split()) try: ans=myCalculator.power(n,p) print(ans) except Exception as e: print(e)