Program to see if all the ones in a number appear on the right side of the number and all zeros appear on the left, how can I do this most efficiently? (i.e. 00000111 is true but 100010 is false)
Given an integer n, write a function that returns count of trailing zeroes in n!. Examples: Input: n = 5 Output: 1 Factorial of 5 is 20 which has one trailing 0. Input: n = 20 Output: 4 Factorial of 20 is 2432902008176640000 which has 4 trailing zeroes. Input: n = 100 Output: 24
//add 1 and check if the result is a power of 2.
ReplyDeleteint check(int n){
if(!(n&(n+1)))return 1;
return 0;
}
@jainendra exacty perfect solution, you can even avoid if condition,you can put the same condition in return statement.
ReplyDeletecheck if n > 0
ReplyDelete