Power of Four

PROBLEM :

Given a number N, check if N is power of 4 or not.

Input:

You have to complete the method which takes 1 argument: the number N  .You should not read any input from stdin/console. There are multiple test cases. For each test case, this method will be called individually.

Output:
Your function should return 1 if N is power of 4, else return 0.

Constraints:
1<=T<=50
1<=N<=100000000

Example:

Input:
2
64
75

Output:
1
0

--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

int isPowerOfFour(unsigned int n)
{
    if(n<4&&(n!=1))
        return 0 ;
    else if(n==1)
        return 1;
    else if(n%4!=0)
        return 0 ;
    else
       return isPowerOfFour(n/4) ;
}

---------------------------------------------------------------------------------

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )