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 :
Question
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 :
Solution
To solve this problem, we can use the bitwise AND operator. The property of numbers that are powers of 2 is that they have exactly one bit set in their binary representation. If we subtract 1 from such a number, we get a number with all bits set after the position of the bit in the original number. So, if we do a bitwise AND of the number and the number minus 1, we should get 0. If we don't, the number is not a power of 2.
Here is a Python solution for the problem:
def is_power_of_two(n):
if n == 0:
return 'NO'
else:
return 'YES' if (n & (n - 1)) == 0 else 'NO'
# Test the function
print(is_power_of_two(20)) # Output: NO
print(is_power_of_two(64)) # Output: YES
print(is_power_of_two(98)) # Output: NO
In this code, the function is_power_of_two checks if the number n is a power of 2. It first checks if n is 0, because 0 is not a power of 2. If n is not 0, it performs a bitwise AND operation between n and n - 1. If the result is 0, it means n is a power of 2, so it returns 'YES'. Otherwise, it returns 'NO'.
Similar Questions
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'.
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
An integer value N is passed as the input. The program must print YES if N is prime number. Else the program must print NO.Input Format:The first line denotes the value of N.Output Format:YES or NO based on if N is a prime number or not. (The OUTPUT is CASE SENSITIVE).Boundary Conditions:2 <= N <= 9999999Example Input/Output 1:Input:19Output:YESExample Input/Output 2:Input:189210Output:NO
Single File Programming QuestionProblem StatementAmil, a curious coder, has a distinctive approach to handling numerical input. Write a program to get an integer from the user. If the number is even, the system employs a goto statement to a label indicating an affirmative response. Otherwise, another goto statement conveys a negative response.Input format :The input consists of a single integer n.Output format :The output displays one of the following:If n is an even number, it displays "You entered an even number."If n is an odd number, it displays "You entered an odd number."Refer to the sample output for the formatting specifications.Code constraints :In the given scenario, the test cases fall under the following constraints:1 ≤ n ≤ 100Sample test cases :Input 1 :1Output 1 :You entered an odd number.Input 2 :100Output 2 :You entered an even number.Input 3 :67Output 3 :You entered an odd number.
Single File Programming QuestionProblem StatementSathish wants to create a program to check whether its digits are in descending order. The program should output "Yes" if the digits are in descending order and "No" otherwise. He wants to get an integer input that has unique digits in it and determine whether the number meets this condition. Write a program to assist Sathish in completing this task using the goto statement.For example, In the number 4321, all the digits are in descending order.Input format :The input consists of an integer N, that represents the number to be checked.Output format :If the digits of N are in descending order, the output prints "Yes".Else, the output prints "No".Refer to the sample output for formatting specifications.Code constraints :In the given scenario, the test cases fall under the following constraints:1 ≤ N < 105Sample test cases :Input 1 :2589Output 1 :NoInput 2 :4321Output 2 :YesInput 3 :93578Output 3 :No
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.