• Question
수를 입력받아 2의 거듭제곱인지 여부를 리턴한다

 

  • Code
public class Question3_powerOfTwo {
    public static void main(String[] args) {
        //수를 입력받는다
        Scanner sc = new Scanner(System.in);
        System.out.println("수를 입력하세요 : ");
        long num = sc.nextInt();

        //2의 거듭제곱인지 확인 후 boolean 값을 출력한다
        while (num >= 1) {
            if (num % 2 == 0 || num == 1) {
                System.out.println(true);                
            } else {
                System.out.println(false);                
            }
            break;
        }
    }
}
  • Result
수를 입력하세요 : 
80
true

+ Recent posts