Power of a number using Java
Find the Power of a Number in Java
Given two integer inputs as number and the power, the objective is to raise the number to a power given. Therefore, we’ll write a Code to Find the Power of a Number in Java Language.
Example Input : 2 3 Output : 8
Find the Power of a Number in Java Language
Given two integers as the number and power inputs, the objective is to raise the number input to the power input and print the value. To do so we’ll use the concepts of loops and recursion. Here are few methods to Find the Power of a Number in Java Language,
- Method 1: Without While Loop
- Method 2: With While Loop
We’ll discuss the above mentioned methods in depth in the upcoming sections.
Method 1: Without While Loop
Java Code
Run
public class Main { public static void main(String[] args) { double base = 1.5; double expo1 = 2.5; double expo2 = -2.5; double res1, res2; // calculates the power res1 = Math.pow(base, expo1); res2 = Math.pow(base, expo2); System.out.println(base + " ^ " + expo1 + " = " + res1 ); System.out.println(base + " ^ " + expo2 + " = " + res2 ); } }
Output
2.0 ^ 2.0 = 4.0
Method 2: With While Loop
Java Code
Run
public class Main { public static void main(String[] args) { double base = 1.5; // Works only when exponent is positive integer int expo = 2; double res = 1.0; while (expo != 0) { res *= base; expo--; } System.out.println("Result = " + res); } }
Output
Result = 2.25
#using recursion
public class powFun {
static int pow(int a, int b){
if(b!=0)
return a*pow(a,b-1);
else
return 1;
}
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int a= sc.nextInt();
int b=sc.nextInt();
int res=pow(a,b);
System.out.println(res);
}
}
Hey !! Kindly join our Discord Community
Join our Discord Community for all your queries.
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int a=in.nextInt();
int b=in.nextInt();
System.out.println(power(a,b));
}
static int power(int x,int n){
if(x==0){
return 0;
}
if(n==0){
return 1;
}
int a=power(x,n-1);
return a*x;
}
}
import java.util.Scanner;
public class PowerofaNum {
public static void main(String[] args) {
Scanner scan= new Scanner(System.in);
System.out.print(“Enter the base :: “);
int base=scan.nextInt();
System.out.print(“Enter the power :: “);
int pow=scan.nextInt();
// Using Loop
int mul=base;
for (int i=1 ; i<pow ; i++){
mul = base * mul;
}
System.out.println("The powered value is :: "+mul);
}
}