Sort by

recency

|

619 Discussions

|

  • + 0 comments
    class Calculator {
        
        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);
            
        }
        
    }
    
  • + 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);
        }
    
    }