Programming Examples

C program to find whether a no is power of two or not.


Write a program to find whether a no is power of two or not.

Power of Two :- In mathematics, a power of two is a number of the form 2^n where n is an integer.

For Example :-

Input = 64

Output = 64 is the power of 2

So as we can see that 2^6 = 64. So the number 64 is the power of two.

Solution

#include <stdio.h>
#include<math.h>
int main() 
{   
	int num;
	printf("Enter Any positive integer Number: ");
	scanf("%d",&num);
	if(ceil(log2(num)) == floor(log2(num)))
	{
		printf("%d is the power of two\n",num);
	}
	else
	{
		printf("%d is not the power of two\n",num);
	}
	return 0; 
} 
Output

Enter Any positive integer Number: 32

32 is the power of two

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

Enter Any positive integer Number: 10

10 is not the power of two

Online Exam Quiz for One day Exam Online Typing Test CCC Online Test 2026 Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level Online Test in Hindi Best Website and Software Company in Allahabad