• + 0 comments

    My Code without Math.pow() function


    import java.util.Scanner;
    
    class MyCalculator {
        public long power(int n, int p) throws Exception {
            if (n == 0 && p == 0) {
                throw new Exception("n and p should not be zero.");
            } else if (n < 0 || p < 0) {
                throw new Exception("n or p should not be negative.");
            } else {
                long temp = 1;
                int count = 1;
                while (count <= p) {
                    temp = temp * n;
                    count++;
                }
                return temp;
            }
    
        }
    }
    
    public class Solution {
        public static void main(String[] args) {
            Scanner s = new Scanner(System.in);
            MyCalculator myCalculator = new MyCalculator();
            while (s.hasNextInt()) {
                int n = s.nextInt();
                int p = s.nextInt();
                try {
                    System.out.println(myCalculator.power(n, p));
                } catch (Exception e) {
                    System.out.println(e);
                }
            }
        }
    }