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