C-Sharp Program- Check If Number is Power of 2

Number is Power of 2-Learn how to calculate whether the given number is power of 2 with explanation and example programs.

Check If Number is Power of 2

The values which are power of 2 is
power(2,0)=1,power(2,1)=2,power(2,2)=4,power(2,3)=8,power(2,4)=16…

We can say the number is power of 2 , only if it is power(2,n)%number==0, -- RULE 1

For example 8 is the number, then power(2,3)%8 ,ie 8%8 is 0.
Based on the Rule 1 , 8 is the number which is a power of 2.

For example 9 is the number, then 9 is the number lies between power(2,3)=8, power(2,4)=16.
i.e, 8%9=8 and 16%9=7.
Based on the Rule 1, 9 is the number which is not a power of 2.

Example Programs

Example Program 1- find whether a no is power of two using while loop.

class Program
    {
        static void Main(string[] args)
        {
          //input number is 7
            int num=7;
            int tempNum, flag;
                      
            tempNum = num;
            flag = 0;
            /*check power of two*/
            while (tempNum != 1)
            {
                if (tempNum % 2 != 0)
                {
                    flag = 1;
                    break;
                }
                tempNum = tempNum / 2;
            }

            if (flag == 0)
                Console.WriteLine("{0} is a number that is the power of 2.", num);
            else
                Console.WriteLine("{0} is not the power of 2.", num);
        }
    }

Output
7 is not the power of 2.

Example Program 2- find whether a number is power of two using functions.

class Program
    {
        static void Main(string[] args)
        {
            int num=7;
            
            
            Program p=new Program();
            bool flag =false;
            flag = p.isPowerOf2(num);    
          
            if (flag == true)
                Console.WriteLine("{0} is a number that is the power of 2.", num);
            else
                Console.WriteLine("{0} is not the power of 2.", num);
        }
        Boolean isPowerOf2(int number)
        {
            while (number != 1)
            {
                if (number % 2 != 0)
                    return false;
                number = number / 2;
            }
            return true;
        }
    }

Output
7 is not the power of 2.

Example Program 3- find whether a number is power of two using functions with bitwise & operator.

 class Program
    {
        static void Main(string[] args)
        {
            int num=7;
            
            
            Program p=new Program();
            bool flag =false;
            flag = p.isPowerOf2(num);    
          
            if (flag == true)
                Console.WriteLine("{0} is a number that is the power of 2.", num);
            else
                Console.WriteLine("{0} is not the power of 2.", num);
        }
        Boolean isPowerOf2(int number)
        {          
            return (number != 0) && ((number & (number - 1)) == 0);            
        }
    }

Output
7 is not the power of 2.

Related Posts