Check if a number can be expressed as a sum of consecutive numbers | Java 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 method
class Test { static boolean canBeSumofConsec ( int n ) { return ( ( ( n & ( n - 1 ) ) != 0 ) && n != 0 ) ; } public static void main ( String [ ] args ) { int n = 15 ; System . out . println ( canBeSumofConsec ( n ) ? " true " : " false " ) ; } }