In java, to calculate power of a number without using pow method we can use loop concepts such as while loop or for loop. For the given base(number) and exponent (power) values, Power is calculated as multiplication of base to base for exponent times.
If the input base is 2 and exponent is 5, output should be 32 (2*2*2*2*2 =32). Here base is 2 which is multiplied itself 5 times.
In this article we will learn writing this program in java and also will see example, detailed logic and program explanation for better understanding.
Required Knowledge
- Java programming basics
- Java Operators
- Loop concepts
Problem statement
We need to write a Java program to calculate power of a number without using pow() method. The program should take base and exponent values as an input from the user. And after calculating the power without using Pow() method, output should be printed.
Let’s see an example,
Input integer number base =2
Input integer number exponent = 5
result = result*base = 2*2*2*2*2= 32
Hence, the number 2 is multiplied by 5 times and gives the result 32.
Logic to calculate power of a number without using pow method
- Take an integer value as base and another integer value as exponent.
- An integer variable result is initialized to 1, which will serve as the accumulator for the calculation.
- A integer variable i acts as a loop counter, initialized to 0.
- Now, A loop (while loop or for loop) iterates as long as i is less than the exponent.
- Inside the loop, the current result is multiplied by the base (result= result*base). This effectively calculates the base raised to the current power represented by i.
- The loop counter i is incremented by 1 (i++).
- After completing loop, the result variable will hold the base raised to the power of the exponent (exponent) and program will print the result.
Calculate power without using pow method in Java
Program 1: Using while loop
In the below program we are using while loop to calculate power of a number without using pow method.
import java.util.Scanner;
public class CalculatePower {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Please enter base");
int base = s.nextInt();
System.out.println("Please enter exponent");
int exponent = s.nextInt();
int result =1;
int i=0;
while(i<exponent) {
result = result*base;
i++;
}
System.out.println("result is: "+result);
s.close();
}
}
Output:
Please enter base
3
Please enter exponent
5
result is: 243
Java program explanation to calculate power of a number without using pow method
- The above java program asks the user to enter the base and exponent.
- Reads the input base and exponent using a Scanner object.
- Then, it initializes a variable
result
to store the result of the power operation, initially set to 1. - Program initializes a variable
i
to 0. - It enters a while loop, where the loop continues as long as
i
is less than the exponent. - Inside the loop, it multiplies the current value of
result
by the base and incrementsi
. - After the loop completes, it prints the calculated result.
- Finally, it closes the Scanner object.
Program 2: Using for loop
In the below program we are using for loop to calculate power of a number without using pow method.
import java.util.Scanner;
public class CalculatePower {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Please enter base");
int base = s.nextInt();
System.out.println("Please enter exponent");
int exponent = s.nextInt();
int result =1;
for(int i=1;i<exponent;i++) {
result = result*base;
}
System.out.println("result is: "+result);
s.close();
}
}
Output:
Please enter base
4
Please enter exponent
4
result is: 256
Java program explanation to calculate power of a number without using pow method
- The above java program asks the user to enter the base and exponent.
- Reads the input base and exponent using a Scanner object.
- It initializes a variable
result
to store the result of the power operation, initially set to 1. - Program enters a for loop, iterating from 1 to one less than value of the exponent.
- In each iteration of the loop, it multiplies the current value of
result
by the base. - After the loop completes, it prints the calculated result.
- Finally, it closes the Scanner object.
Conclusion
In this article, we have learnt writing the java program to calculate power of a number without using pow method. We understood the logic and seen the program using the while loop in Java and for loop in Java. Hope this tutorial helped you in the understanding of this program.
You also check our another awesome tutorials:
- Java Program to check Perfect number
- Program to print sum of all the even numbers in a range in Java
- Java program to find Strong numbers in a given range
- Check if a triangle is equilateral, isosceles or scalene in Java
Happy Coding!