Provided list of Simple Java Programs is specially designed for freshers and beginners to get familiarize with the concepts of Java programming language and become pro in coding.
Program to Calculate the Power of a Number
In this article we will see how to calculate the power of a number.
Power of a number is the number raised to that power i.e. when we multiply a number several times to itself.
For example 2^5 = 2*2*2*2*2 = 32
Different methods to do it.
Let’s see one by one.
Method-1 : Using a For Loop
We can find the power of a number by using a for loop which will iterate over and over until the iterator reaches zero.
Approach:
- We will take the base and the power as input and store them in variables. Also we will initialize the result variable to 1.
- Then we will store the power into an iterator variable.
- Then we will iterate over the loop and keep on multiplying the base to itself.
- Finally we will print the result.
Program:
import java.util.Scanner; class powerNum{ public static void main(String args[]) { System.out.println("Enter the base and the power"); //Here we are initializing the number and then //take them as input Scanner scan = new Scanner(System.in); int base = scan.nextInt(), power = scan.nextInt(); long res = 1; //We will copy the power into the iterators and then run the loop int i = power; for (;i != 0; --i) { res *= base; } //Printing the output System.out.println("The value of "+base+" raised to the power "+power+" is "+res); } }
Output: Enter the base and the power 2 3 The value of 2 raised to the power 3 is 8
Method-2 : Using a while Loop
We can find the power of a number by using a while loop which will iterate over and over until the iterator equals the power.
Approach:
- We will take the base and the power as input and store them in variables. Also we will initialize the result variable to 1.
- Then we will set the iterator to 0 and increment it for every iteration.
- For each iteration we will be multiplying the number to itself and storing it in result.
- Finally we will print the result.
Program:
import java.util.Scanner; class powerNum { public static void main(String args[]) { System.out.println("Enter the base and the power"); //Here we are initializing the number and then //take them as input Scanner scan = new Scanner(System.in); int base = scan.nextInt(), power = scan.nextInt(); long res = 1; //We will start the loop from 0 and increment it for every iteration //until it becomes equal to the power int i = 0; while(i<power) { i++; res *= base; } //Printing the output System.out.println("The value of "+base+" raised to the power "+power+" is "+res); } }
Output: Enter the base and the power 2 3 The value of 2 raised to the power 3 is 8
Method-3 : Using recursion
Using recursion we can also get the power of a number.
Approach:
- We will take the
base
and thepower
as input and store them in variables. - Then we will pass the power and base to a recursive function.
- The function will return 1 if the power is 0, or else it will call the same function with decrementing power.
- Finally we will print the result.
Program:
import java.util.Scanner; class powerNum{ public static int powerCalc(int base, int power) { //Recursive Function if (power == 0) return 1; else return base * powerCalc(base, power - 1); } public static void main(String args[]) { System.out.println("Enter the base and the power"); //Here we are initializing the number and then //take them as input Scanner scan = new Scanner(System.in); int base = scan.nextInt(), power = scan.nextInt(), res; //Passing the argument into function res = powerCalc(base, power); //Printing the output System.out.println("The value of "+base+" raised to the power "+power+" is "+res); } }
Output: Enter the base and the power 25 5 The value of 25 raised to the power 5 is 9765625
Method-4 : Using a Library Package
Java has a math library that contains a function called pow( )
which returns the value of the number raised to a certain power.
Approach:
- We will take the
base
and thepower
as input and store them in variables. - Then we will pass the power and base to the library function
pow( )
. - The function will return the value which will be stored in another variable.(We will use typecasting to convert the number to int as the library function returns a floating point number)
- Finally we will print the result.
Program:
import java.util.Scanner; import java.lang.Math; class Main { public static void main(String args[]) { System.out.println("Enter the base and the power"); //Here we are initializing the number and then //take them as input Scanner scan = new Scanner(System.in); int base = scan.nextInt(), power = scan.nextInt(), res; //Passing the argument into function res = (int)Math.pow(base, power); //Printing the output System.out.println("The value of "+base+" raised to the power "+power+" is "+res); } }
Output: Enter the base and the power 2 3 The value of 2 raised to the power 3 is 8
Understand the Programming Language Java and learn the language fastly by using our wide range of Java Programming Examples with Output and try to write programs on your own.
Related Java Decision Making and Loop Programs:
- Java Program to Check Leap Year
- Java Program to Check Whether a Number is Positive or Negative
- Java Program to Check Whether a Character is Alphabet or Not
- Java Program to Calculate the Sum of Natural Numbers
- Java Program to Find Factorial of a Number
- Java Program to Generate Multiplication Table
- Java Program to Find GCD of two Numbers
- Java Program to Find LCM of two Numbers
- Java Program to Display Alphabets (A to Z) using loop
- Java Program to Count Number of Digits in an Integer
- Java Program to Check Palindrome
- Java Program to Check Whether a Number is Prime or Not
- Java Program to Check Armstrong Number
- Java Program to Display Armstrong Number Between Two Intervals
- Java Program to Make a Simple Calculator Using switch…case
- Java Program to Sort Elements in Lexicographical Order (Dictionary Order)