Function to find the given number is a power of 2 or not?
Answer Posted / s.v.prasad reddy,lifetree conv
#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
int num,i=0,flag;
clrscr();
printf("\nEnter the number:=");
scanf("%d",&num);
if(num==0 || num==1 || (num%2)!=0) /* Validation Part */
{
printf("\n We can't express the given number to power
of 2");
return;
}
for(;;}
{
if(pow(2,i)==num)
{
flag=1;
break;
}
if(pow(2,i)>num)
{
flag=0;
break;
}
i++;
}
if(falg==1)
printf("\n %d number is 2 power of %d",num,i);
else
printf("\n%d number can't be expressed as power of 2",num);
getch();
}
| Is This Answer Correct ? | 3 Yes | 4 No |
Post New Answer View All Answers
where are auto variables stored? What are the characteristics of an auto variable?
swap 2 numbers without using third variable?
What is the advantage of using #define to declare a constant?
What is sizeof int in c?
Can you define which header file to include at compile time?
How do you define a function?
The __________ attribute is used to announce variables based on definitions of columns in a table?
What are different types of pointers?
What is || operator and how does it function in a program?
What is c variable?
Is linux written in c?
What is keyword in c?
Explain what are compound statements?
Explain built-in function?
What is the purpose of main() function?