power of 2 in java code example

Example 1: java is power of 2

boolean result = number > 0 && ((number & (number - 1)) == 0);

Example 2: Java program to find the power of a number

@Test
	public void powerOfTheNumberProgram13() {
		Scanner scanner = new Scanner(System.in);
		System.out.println("Enter any NUMBER: ");
		int number = scanner.nextInt();
		System.out.println("Enter the POWER: ");
		int power = scanner.nextInt();
		scanner.close();
		// Logic for finding the POWER of number
		int powerOfNumber = (int) Math.pow(number, power);
		System.out.println("Power of a " + number + "^" + power + ": " + powerOfNumber);
	}

Example 3: how to find powers in java

int n=Math.pow(N, P);

Tags:

Java Example