- Related Questions & Answers
- Check if a number can be expressed as x^y (x raised to power y) in C++
- Check if a number can be expressed as power in C++
- Check if a number can be expressed as a^b in C++
- Check if a number can be expressed as sum two abundant numbers in C++
- Check if a number can be represented as a sum of 2 triangular numbers in C++
- Check if a number can be expressed as a sum of consecutive numbers in C++
- Check if a number can be represented as sum of non zero powers of 2 in C++
- C++ Program to Check Whether a Number can be Expressed as Sum of Two Prime Numbers
- Find the value of the function Y = (X^6 + X^2 + 9894845) % 981 in C++
- Maximum number of 2×2 squares that can be fit inside a right isosceles triangle in C
- How to check if a number is a power of 2 in C#?
- Write a C# program to check if a number is divisible by 2
- Sum of the Series 1 + x/1 + x^2/2 + x^3/3 + .. + x^n/n in C++
- Check if a number can be written as sum of three consecutive integers in C++
- Program to find sum of 1 + x/2! + x^2/3! +…+x^n/(n+1)! in C++

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Here we will see, if we can represent a number as sum of two non-zero powers of 2. So we will check the given number N can be represented as (2^{x} + 2^{y}) where x, y > 0. Suppose a number is 10, this can be represented as 2^{3} + 2^{1}.

The approach is simple. There are two cases. If the number n is even, it can be represented as 2^{x}. Where x > 0. Another case is that is N is odd, it can never be represented as sum of powers of 2. We cannot use power as 0, so we cannot get odd numbers. for all odd numbers LSb of its binary representation is 1

#include <iostream> using namespace std; bool isSumofTwosPower(int n) { if((n & 1) == 0){ return true; }else{ return false; } } int main() { int num = 86; if(isSumofTwosPower(num)){ cout << "Can be represented"; }else{ cout << "Cannot be represented"; } }

Can be represented

Advertisements