Skip to content

Commit

Permalink
Create Power_of_2.cpp
Browse files Browse the repository at this point in the history
This code checks weather the given number is a power of 2 or not.
  • Loading branch information
ADARSH-863 committed Oct 1, 2022
1 parent 9b30e69 commit 7ca2e29
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions C++/Power_of_2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Author: ADARSH
// Date Modified: 01/10/2022

#include <bits/stdc++.h>
using namespace std;

bool isPowerOf2(int n) {
return (n && !(n & (n - 1)));
}

int main(int argc, char const *argv[])
{
int n;
cout << "Enter a number: ";
cin >> n;
cout << ((isPowerOf2(n)) ? "It's a power of 2." : "It's not a power of 2.");
return 0;
}

0 comments on commit 7ca2e29

Please sign in to comment.