*In this post, we will discuss how to find power of a number in java. e.g NP(Where N is the base, P is the Power & Multiply N for P times to find the power of NP)*

As we have taken an example of a number ‘**N**‘ and power is ‘**P**‘, so to find N to the power of P, we can multiply N for P times manually, but if the value of ‘**P**‘ will be a big number, then it is much more difficult to find the result. In this case, we can also use the predefined function **pow(base, power) **to find a power of a number.

In this post, we have given all **3 simple programs** to find the power of a number in Java and also to cover above all scenarios.

**Example of power of number:**

Here. we have taken an example of the power of a number like **10****4**.

**10****4** = 10 X 10 X 10 X 10 = 10000

Where **N = 10** (Given Number) and **P = 4** (Power or Exponent)

Let’s the programs for the power of a number in Java.

**How To Find Power Of A Number In Java?**

We can find the power of a number in java by using the below 3 approaches:

- Find power of a number using for loop.
- Find power of a number using while loop.
- Find power of a number using pow( ) function.

Read Also:How To Reverse A String In Java – Learn 5 Easy Methods

**Approach-1: How to find power of a number in Java using for loop**

Here, We have taken for loop for(int i = 1; i <= power; i++) to iterate from **1 **to** power**(In the example we have taken 4, So from **1 to 4**) given by the user.

As in the above example, we have multiplied 10 for 4 times to get the result. Similarly, by using for loop, we will multiply result with base and then store this value result again. We will continue this loop until i value becomes equal to the power value.

We have taken result = 1. So in the first loop, result = 1 * 10 = 10. This value ’10’ will be store in the result again and in the 2nd loop, result = 10 * 10 = 100. Now. 100 will be stored in result. Similarly for loop will continue until i value becomes power value.

**Java Program using for loop:**

```
/**
* java program to find power of a number using while loop
*/
package cf.java.num;
import java.util.Scanner;
public class PowerOfANumber {
public static void main(String[] args) {
// Declare variables
int base = 0, power = 0, result = 1;
Scanner sc = new Scanner(System.in);
// Accept base value
System.out.print("Enter base value: ");
base = sc.nextInt();
// Accept power value
System.out.println("Enter power value: ");
power = sc.nextInt();
// Use for loop to find the power of a number
for(int i = 1; i <= power; i++) {
result = result * base;
}
// Display the power of a number
System.out.println("Hence, " + base + " to the power of " + power + " is: " + result);
}
}
```

Code language: Java (java)

**Output:**

`Enter base value: 10 Enter power value: 4 Hence, 5 to the power of 4 is: 10000`

Read Also:Palindrome In Java(String & Number) – Check 2 Easy Methods

**Approach-2(Java program to find the power of a number using while loop):**

Here, We have taken while loop while(power != 0) to iterate till power value becomes 1.

Similarly, as we discussed in **for loop program**, we will multiply the result with base value up to N times(In the example we have taken N = 4) and in the program, we have taken until power != 0).

**Java Program using while loop:**

```
/**
* java program to find power of a number using while loop
*/
package cf.java.num;
import java.util.Scanner;
public class PowerOfANumber {
public static void main(String[] args) {
// Declare variables
int base = 0, power = 0, result = 1;
Scanner sc = new Scanner(System.in);
// Accept base value
System.out.print("Enter base value: ");
base = sc.nextInt();
// Accept power value
System.out.println("Enter power value: ");
power = sc.nextInt();
// Use while loop to find power of number
while(power != 0) {
result = result * base;
power--;
}
// Display the power of a number
System.out.println("Hence, " + base + " to the power of " + power + " is: " + result);
}
}
```

Code language: PHP (php)

**Output:**

`Enter base value: 10 Enter power value: 4 Hence, 5 to the power of 4 is: 10000`

Read Also:Java Program To Print Vowels In A String

**Approach-3: Java program to find the power of a number using Math.pow( ) function**

In this program, we have taken the predefined function pow( ) of **Math** class. We can find the power of a given number directly by using pow( ) function.

**Syntax for pow( ) function:**

double java.lang.Math.pow(double base, double power);

By default pow( ) function returns double type value. We can convert this double value to an integer value by using casting(result = (int) Math.pow(base, power);).

Click here to know more about Math.pow( ) function.

**Java Program using Math.pow( ) function:**

/** * java program to find power of a number using Math.pow( ) function */ package cf.java.num; import java.util.Scanner; public class PowerOfANumber { public static void main(String[] args) { // Declare variables int base = 0, power = 0, result = 1; Scanner sc = new Scanner(System.in); // Accept base value System.out.print(“Enter base value: “); base = sc.nextInt(); // Accept power value System.out.println(“Enter power value: “); power = sc.nextInt(); // Use Math.pow( ) funtion result = (int) Math.pow(base, power); // Display power of a number System.out.println(“Hence, ” + base +” to the power of “+ power + ” is: “+ result); } }

```
/**
* java program to find power of a number using while loop
*/
package cf.java.num;
import java.util.Scanner;
public class PowerOfANumber {
public static void main(String[] args) {
// Declare variables
int base = 0, power = 0, result = 1;
Scanner sc = new Scanner(System.in);
// Accept base value
System.out.print("Enter base value: ");
base = sc.nextInt();
// Accept power value
System.out.println("Enter power value: ");
power = sc.nextInt();
// Find power of the number
result = (int) Math.pow(base, power);
// Display the power of a number
System.out.println("Hence, " + base + " to the power of " + power + " is: " + result);
}
}
```

Code language: JavaScript (javascript)

**Output:**

`Enter base value: 10 Enter power value: 4 Hence, 5 to the power of 4 is: 10000`

**Other Readers also ask for:**

- How to find power of a number in java?
- how to find power of a number in java?
- how to calculate power in java?
- how to calculate power of a number in java?
- how to use power in java?

## 2 thoughts on “How To Find Power Of A Number In Java – 3 Simple Ways”