You are on page 1of 2

POWER OF 2

How to find if a number is power of 2?


AVAILABLE
INPUT

num=0, other=1

PROCESSING

REQUIRED
OUTPUT

Number is a prime or not a prime number

SOURCE CODE:
import java.io.*;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class PowerOfTwo
{
public static void main(String args[]) throws IOException
{
String in;
in = JOptionPane.showInputDialog("Enter the number : ");
int num = Integer.parseInt(in);
int other = 1;
if(((~num) & 1) == 1)
{
JOptionPane.showMessageDialog(null, num + " is a Power of 2", "IDENTFYING
NUMBER IS A POWER OF 2",
JOptionPane.INFORMATION_MESSAGE);
}
else
{
JOptionPane.showMessageDialog(null, num + " is not a Power of 2", "IDENTFYING
NUMBER IS A POWER OF 2",
JOptionPane.INFORMATION_MESSAGE);
}
}
}

SCREENSHOTS

You might also like