How to calculate the Nth power of a number in Java

2 Answers

0 votes
public class MyClass {
    public static void main(String args[]) {
        double number = 4;
        int N = 5;
        
        double power = Math.pow(number, N);

        System.out.println(power);
    }
}



/*
run:

1024.0

*/

 



answered Mar 31, 2021 by avibootz
0 votes
public class MyClass {
    public static void main(String args[]) {
        double number = 4;
        int N = 5;
        
        double power = 1;
        
        for (int i = 0; i < N; i++){
            power *= number;
      }

        System.out.println(power);
    }
}



/*
run:

1024.0

*/

 



answered Mar 31, 2021 by avibootz

Related questions

1 answer 296 views
1 answer 242 views
242 views asked Mar 26, 2021 by avibootz
1 answer 226 views
2 answers 269 views
269 views asked May 22, 2017 by avibootz
1 answer 85 views
1 answer 142 views
...