## 4 Ways to Calculate Power of Number in Java

Here I have shared the 4 different ways to calculate power of number in Java. Using Loop Using Recursion Using Math.pow() Using BigInteger.pow() 4 Ways to Calculate Power of Number in Java 1. Using Loop We can calculate the power of number by repeatedly multiplying the number by itself in a loop. This method works only for positive integer power.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package com; import java.util.Scanner; public class PowerOfNumber { public static void main(String args[]) { int n, p, result=1, i; Scanner sc = new Scanner(System.in); System.out.println("Enter a number and its power:"); n = sc.nextInt(); p =sc.nextInt(); for(i = 1; i <= p; ++i){ result = result * n; } System.out.println(result); } } |

Output Enter a number and its power: 3 3 27 2. Using Recursion Recursion can also be

Read more