Home >>Cpp Programs >Program to find whether a no is power of two in CPP

Program to find whether a no is power of two in CPP

C++ program to check if number is power of 2 using Bitwise operator

In this example, we will see a C++ program through which we can check if the given number is power of 2 or not.

In this program, we will divide the given number by 2 until the number will not get 0 or 1. This algorithm takes the O(log(n)) complexity to solve this problem.

Program:

#include <iostream>
using namespace std;
int main()
{
// declaring the array n
int n[]={4,9,15,16,20,22,25,32,45,64,72,80,100,128,256};
int i;

for(i=0;i<15;i++)
{
cout<< n[i] << " is power of 2 : ";
// use of bitwise AND (&) operator
if((n[i]& (n[i]-1))==0) 
cout<<"True"<<endl;
else
cout<<"False"<<endl;
}
return 0;	
}

Output:
4 is power of 2 : True
9 is power of 2 : False
15 is power of 2 : False
16 is power of 2 : True
20 is power of 2 : False
22 is power of 2 : False
25 is power of 2 : False
32 is power of 2 : True
45 is power of 2 : False
64 is power of 2 : True
72 is power of 2 : False
80 is power of 2 : False
100 is power of 2 : False
128 is power of 2 : True
256 is power of 2 : True

Cpp Programs Fibonacci Series in C++ Armstrong Number in C++ Factorial program in C++ Check Palindrome in C++ Prime Number Program In C++ Reverse Number Program in C++ Sum Of Digits Program in C++ C++ Find C++ protected keyword CPP Program for different ways to print array elements CPP Program to determine the colour of chess square CPP Program to Reverse Number CPP Program to Calculate Power of a Number CPP Program to print all Even and Odd numbers from 1 to N Program to find whether a no is power of two in CPP C++ program to find largest list of prime numbers Auto keyword in Cpp C++ program to print the left Rotation of the array Convert a given binary Tree to Doubly Linked List Delete keys in a Linked list using C++ program How do you delete a linked list in C++ Implement stack with linked list in c++ C++ Program to find first occurrence of a Number Using Recursion in an array C++ program to find Last occurrence of a Number using Recursion in an array C++ Program to find Union of two singly Linked Lists Remove Duplicates from linked list in c++ C++ Program to find Nth node in Linked List Merge sort singly linked list c++ C++ Program to Convert Roman Number to Integer Number C++ Program to find LCM of two numbers C++ Password Generator C++ Program to multiply two numbers without using multiplication operator C++ Program to print all possible subset of a set Sum of all the elements in an array divisible by a given number Print First uppercase letter in a string c++ C++ Program to Find the Number Occurring Odd Number of Times C++ Program for Print address of Variable Using Pointer C++ Program to Find ASCII Value of a Character C++ Classes and Objects Program Create a class method in C++ C++ Create Empty Class Define a class method outside the class definition in C++ How to create multiple objects of a class in C++
No Sidebar ads