Check if a number can be expressed as a sum of consecutive numbers | C ++ program to check if a number can be expressed as sum of consecutive numbers ; This function returns true if n can be expressed sum of consecutive . ; We basically return true if n is a power of two ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool canBeSumofConsec ( unsigned int n ) { return ( ( n & ( n - 1 ) ) && n ) ; } int main ( ) { unsigned int n = 15 ; canBeSumofConsec ( n ) ? cout << " true " : cout << " false " ; return 0 ; }