# How To Find Power Of A Number In Java – 3 Simple Ways

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 104.

104 = 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:

1. Find power of a number using for loop.
2. Find power of a number using while loop.
3. Find power of a number using pow( ) function.

### 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:

```.wp-block-code {
border: 0;
}

.wp-block-code > div {
overflow: auto;
}

.shcb-language {
border: 0;
clip: rect(1px, 1px, 1px, 1px);
-webkit-clip-path: inset(50%);
clip-path: inset(50%);
height: 1px;
margin: -1px;
overflow: hidden;
position: absolute;
width: 1px;
word-wrap: normal;
word-break: normal;
}

.hljs {
box-sizing: border-box;
}

.hljs.shcb-code-table {
display: table;
width: 100%;
}

.hljs.shcb-code-table > .shcb-loc {
color: inherit;
display: table-row;
width: 100%;
}

.hljs.shcb-code-table .shcb-loc > span {
display: table-cell;
}

.wp-block-code code.hljs:not(.shcb-wrap-lines) {
white-space: pre;
}

.wp-block-code code.hljs.shcb-wrap-lines {
white-space: pre-wrap;
}

.hljs.shcb-line-numbers {
border-spacing: 0;
counter-reset: line;
}

.hljs.shcb-line-numbers > .shcb-loc {
counter-increment: line;
}

.hljs.shcb-line-numbers .shcb-loc > span {
}

.hljs.shcb-line-numbers .shcb-loc::before {
border-right: 1px solid #ddd;
content: counter(line);
display: table-cell;
text-align: right;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
white-space: nowrap;
width: 1%;
}```/**
* 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``````

### 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);).

#### 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``````