# how to check that given number is power of 2 or not

## how to check that given number is power of 2 or not

In this article we are going to learn that how to check that a given number is power of 2 or not .

**example :-**2= 2^1

4=2^2

8= 2^3 and so on .

__method 1:-__ In this method we will check by using log base 2 if

**ceil**and**floor**of log base of the number is same then number is a power of 2. ceil(3.56)=4

ceil(4.23)=5

floor(3.56)=3

floor(4.23)=4

__Code:-__#include <stdio.h> #include<math.h> int main() { int n; printf("Enter a number\n"); scanf("%d",&n); // logic for checking that // number is power of 2 or not_ if (ceil(log2(n))==floor(log2(n))) printf("Number is in power of 2"); else printf("Number is not in power of 2"); return 0; }

__Output:-__Enter a number 8 Number is in power of 2

** Method 2:-** (keep diving by 2 until n become 1)

Keep divide the number by 2, until n becomes 1 ,(i.e, do n = n/2 iteratively). In any iteration, if n%2 becomes non-zero (i.e n is not divisible by 2) and n is not 1 then n is not a power of 2. If n becomes 1 then it is a power of 2.

__Code:-__#include <stdio.h> int main() { int n,r,flag; printf("Enter a number\n"); scanf("%d",&n); // logic for checking that // number is power of 2 or not_ flag=0; while(n!=1) { r=n%2; if(r!=0) { flag=1; break; } n=n/2; } if(flag==1) printf("Number is not power of 2"); else printf("Number is power of 2"); return 0; }

__Output:-__

Enter a number 8 Number is in power of 2

__Recommended Post:__

- c program to convert specified days into years weeks and days

- Print Reverse Hollow Pyramid
- Update the booking ID | Wipro previous year question paper
- Pages in PDF | Wipro previous year question paper
- Sparse Matrix in data structure
- Find the location ID | Wipro previous year Coding question
- find the odd digits | Wipro Coding question
- Find the product id | Wipro Coding question
- Difference between static and dynamic memory allocation
- What is asymptotic Notation

__Cracking the coding interview:-__

__Array and string:-__

__Tree and graph:-__

__Hackerearth Problems:-__

- Very Cool numbers | Hacker earth solution
- Vowel Recognition | Hackerearth practice problem solution
- Birthday party | Hacker earth solution
- Most frequent | hacker earth problem solution
- program to find symetric difference of two sets
- cost of balloons | Hacker earth problem solution
- Chacha o chacha | hacker earth problem solution
- jadu and dna | hacker earth solution
- Bricks game | hacker earth problem
- Anti-Palindrome strings | hacker earth solution
- connected components in the graph | hacker earth data structure
- odd one out || hacker earth problem solution
- Minimum addition | Hackerearth Practice problem
- The magical mountain | Hackerearth Practice problem
- The first overtake | Hackerearth Practice problem

__Hackerrank Problems:-__- Playing With Characters | Hackerrank practice problem solution
- Sum and Difference of Two Numbers | hackerrank practice problem solution

- Functions in C | hackerrank practice problem solution

- Pointers in C | hackerrank practice problem solution

- Conditional Statements in C | Hackerrank practice problem solution

- For Loop in C | hackerrank practice problem solution

- Sum of Digits of a Five Digit Number | hackerrank practice problem solution

- 1D Arrays in C | hackerrank practice problem solution

- Array Reversal | hackerrank practice problem solution

- Printing Tokens | hackerrank practice problem solution

- Digit Frequency | hackerrank practice problem solution

- Calculate the Nth term | hackerrank practice problem solution

__Data structure:-__

- Program to find cycle in the graph
- Implementation of singly link list
- Implementation of queue by using link list
- Algorithm of quick sort
- stack by using link list
- program to find preorder post order and inorder of the binary search tree
- Minimum weight of spanning tree
- Preorder, inorder and post order traversal of the tree

__Key points:-__

- How to set limit in the floating value in python

- What is boolean data type
- How to print any character without using format specifier

Must check this:-

__ MCQs:-__