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
public class PowerOfNumber {
public static void main(String[] args) {
int num = 2;
int power = 4;
int ans = 1;
if (num == 0 && power == 1) {
System.out.println(“The power of number ” + num + ” to the power ” + power + ” is 1″);
} else if (num >= 0 && power == 0) {
System.out.println(“The power of number ” + num + ” to the power ” + power + ” is 1″);
} else if (num >= 0 && power == 1) {
System.out.println(“The power of number ” + num + ” to the power ” + power + ” is ” + num);
} else if (num == 0 && power == 0) {
System.out.println(“The power of number ” + num + ” to the power ” + power + ” is indeterminate”);
} else {
for (int i = 1; i <= power; i++) {
ans *= num;
}
System.out.println("The power of number " + num + " to the power " + power + " is " + ans);
}
}
}
The above code also checks the power of number like 0 and 1 …
package Numbers;
public class PowerOfNumber {
public static void main(String[] args) {
int base=5;
int pow=5;
int result=1;
for(int i=1;i<=pow;i++){
result=result*base;
}
System.out.println(result);
}
}
package Numbers;
public class ArmstrongNumberInRange {
public static void main(String[] args) {
int a=100;
int b=1000;
for(int i=0;i0){
System.out.println(“null”);
}
}
}
}
class Main {
public static void main(String[] args) {
System.out.println(“Enter a number and there poewr”);
Scanner sc = new Scanner(System.in);
int num=sc.nextInt();
int power=sc.nextInt();
int result=(int)Math.pow(num,power);
System.out.println(result);
}
}
package hellopackage;
import java.util.Scanner;
public class power {
static int mypower(int b,int p) {
int result=1;
for(int i=1;i<=p;i++) {
result*=b;
}
return result;
}
public static void main(String[] args) {
Scanner obj=new Scanner(System.in);
System.out.println("enter the base number");
int base=obj.nextInt();
System.out.println("enter the power number");
int power=obj.nextInt();
System.out.println("power of the number "+mypower(base,power));
}
}
using Array
int arr[]=new int[power];
int mul2=1;
int print=0;
System.out.println(num+” “+power);
for(int i=0;i<=arr.length-1;i++)
{
arr[i]=num;
mul2=mul2*arr[i];
print=mul2;
}
System.out.println("result: "+print);
class I
{
public int power(double num,double p){
double powValue=Math.pow(num, p);
int result = (int) powValue;
return result;
}
public static void main (String[]args){
I ob=new I();
int res=ob.power(3,4);
System.out.println(res);
}
}
public class power {
public static void main(String[] args) {
int n = 2;
int power = 3;
int result =1;
for(int i =1; i<=power;i++)
{
result= n * result;
}
System.out.println("the power of " + n + " is " + result);
}
}
#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);
}
}