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

Latest Current Affairs 2024 Online Exam Quiz for One day Exam Online Typing Test CCC Online Test 2024 Python Programming Tutorials 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 Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Sarkari Naukari Notification Best Website and Software Company in Allahabad Sarkari Exam Quiz