Knowee
Questions
Features
Study Tools

Find whether the given number is power of 2 or not.Input Format:Accept an Integer as inputOutput Format:Print the output as "YES" or "NO"Constraints:1<=INPUT<=10^15Sample Input 1:128Sample Output 1:YESSample Input 2:36Sample Output 2:NO

Question

Find whether the given number is power of 2 or not.Input Format:Accept an Integer as inputOutput Format:Print the output as "YES" or "NO"Constraints:1<=INPUT<=10^15Sample Input 1:128Sample Output 1:YESSample Input 2:36Sample Output 2:NO

🧐 Not the exact question you are looking for?Go ask a question

Solution

To determine whether a given number is a power of 2 or not, follow these steps:

  1. Accept an integer as input.
  2. Check if the input number is equal to 1. If it is, then it is a power of 2. Print "YES" and exit.
  3. If the input number is not equal to 1, check if it is divisible by 2. If it is not, then it is not a power of 2. Print "NO" and exit.
  4. If the input number is divisible by 2, divide it by 2 and repeat step 3.
  5. Continue dividing the number by 2 until it becomes 1 or is no longer divisible by 2.
  6. If the number becomes 1, then it is a power of 2. Print "YES".
  7. If the number is no longer divisible by 2 and is not equal to 1, then it is not a power of 2. Print "NO".

Here is an example implementation in Python:

def is_power_of_two(num):
    if num == 1:
        return True
    elif num % 2 != 0:
        return False
    else:
        return is_power_of_two(num // 2)

# Accept input from the user
num = int(input("Enter a number: "))

# Check if the number is a power of 2
if is_power_of_two(num):
    print("YES")
else:
    print("NO")

Sample Input 1: 128 Sample Output 1: YES

Sample Input 2: 36 Sample Output 2: NO

This problem has been solved

Similar Questions

Single File Programming QuestionProblem StatementGiven a non-negative integer N, the task is to check if N is a power of 2. Print 'YES' if N is the power of 2; else, print 'NO'.Example 1Input: N = 8Output: YESExplanation: 8 is equal to 2 raised to 3 (23 = 8).Example 2Input: N = 98Output: NOExplanation: 98 cannot be obtained by any power of 2.Note: This question was asked in Adobe coding test.Input format :The input consists of an integer N, representing the non-negative integer.Output format :If N is a power of 2, the output is "YES".If N is not a power of 2, the output is "NO".Refer to the sample output for formatting specifications.Code constraints :In this scenario, the test cases fall under the following constraints:1 ≤ N ≤ 512Sample test cases :Input 1 :20Output 1 :NOInput 2 :64Output 2 :YESInput 3 :98Output 3 :NONote :The program will be evaluated only after the “Submit Code” is clicked.Extra spaces and new line characters in the program output will result in the failure of the test case.Marks :

Given a non-negative integer N, the task is to check if N is a power of 2. Print 'YES' if N is the power of 2; else, print 'NO'.

Given an integer input , whether the given input is "Positive" or "Negative" or "Zero" and print the corresponding messageInput Format:Enter an integer as a input  Output Format: Print the output as "Negative" or "Positive" or "Zero"Constraints:1 <= INPUT <= 10^15Sample Input 1:-98Sample Output 1:NEGATIVESample Input 2:0Sample Output 2:ZERO

Given 2 integer input check whether the 2 input are "Equal" or "Not Equal" and print the corresponding message.Input Format:Accept two integers as  inputOutput Format:Print the output as "Equal" or "Not Equal"Constraints:1 <= INPUT <= 10^15

Write a program to check the given number is perfect or not ?Constraints:1<n<1000Input FormatAn integer nOutput FormatA corresponding stringExample:Input 1:6Output 1:YesInput 2:3Output 2:No

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.