Give a one-line C expression to test whether a number is a
power of 2.

Answers were Sorted based on User's Feedback

Give a one-line C expression to test whether a number is a power of 2...

Answer / lakshmi

void main()
{
int a;
scanf("%d",&a);

if((a&a-1)==0)
printf("Is a power of 2");
else
printf("Not a power of 2");
}

 Is This Answer Correct ? 31 Yes 8 No

Give a one-line C expression to test whether a number is a power of 2...

Answer / fallen angel

if(x&(x-1)==0)
then TRUE;
else
FALSE;

use the bitwise and operator

 Is This Answer Correct ? 20 Yes 2 No

Give a one-line C expression to test whether a number is a power of 2...

Answer / mayur shankariya

#include<stdio.h>
main()
{
int num;
printf("Enter Number
");
scanf("%d",&num);
(num & num - 1)? printf("Not
"):printf("Power of two
");
}

 Is This Answer Correct ? 1 Yes 0 No

Give a one-line C expression to test whether a number is a power of 2...

Answer / rarach

if( (a == 2 ) || ((a/2) %2 == 0 ) && a !=1)
TRUE
else
false

 Is This Answer Correct ? 1 Yes 1 No

Give a one-line C expression to test whether a number is a power of 2...

Answer / rohit

if((log(n)/log(2))/floor(log(n)/log(2))==1)
then TRUE;
else FALSE;

 Is This Answer Correct ? 1 Yes 5 No

Give a one-line C expression to test whether a number is a power of 2...

Answer / ramesh b penchal

#include<stdio.h>
main()
{
int n,i,d,m;
printf("Enter a number");
scanf("%d",&n);
m=n;
while(n>0)
{
d=n%10;
if(d!=0)
{
prntf("%d is not power of 2",m);
getch();
exit();
}
n=n/10;
}
prntf("%d is power of 2",m);
getch();
}

 Is This Answer Correct ? 0 Yes 4 No

Give a one-line C expression to test whether a number is a power of 2...

Answer / santhoo035

#include<iostream.h>
int main()
{
int n,i;
cout<<"Enter a number";
cin>>n;
for(i=1;i<n/2;i++)
{
if((2<<i)==n)
{
cout<<"The given no is power of 2";
break;
}
}
}

 Is This Answer Correct ? 3 Yes 8 No

Give a one-line C expression to test whether a number is a power of 2...

Answer / akshay rastogi

#include<conio.h>
void main()
{
int n,i=0,num=0;
printf("\n enter any number");
scanf("%d",&n);
while(num<=n)
{
i++;
num=2^i;
}
if(num==n)
printf("yes no. is power of 2");
else
printf("no.");
}

 Is This Answer Correct ? 0 Yes 6 No

Give a one-line C expression to test whether a number is a power of 2...

Answer / abc def

/*following expr evaluates to true if num is a power of
2.Else it's false. '&' - bitwise and.*/
(num == 1) || !(num & 1)

 Is This Answer Correct ? 0 Yes 9 No

Give a one-line C expression to test whether a number is a power of 2...

Answer / trailokya ranjan jena

#include<stdio.h>
void main()
{
int i,j;
clrscr();
printf("\n Enter a num");
scanf("%d",&i);
j=i;
for(;i%2==0;i/=2);
if(i==1)
printf("\n%d is power of 2",j);
else
printf("\n%d is not a power of 2");
getch();
}

 Is This Answer Correct ? 4 Yes 18 No

More C Code Interview Questions

int i; main(){ int t; for ( t=4;scanf("%d",&i)-t;printf("%d\n",i)) printf("%d--",t--); } // If the inputs are 0,1,2,3 find the o/p

Given an array of size N in which every number is between 1 and N, determine if there are any duplicates in it. You are allowed to destroy the array if you like.

How we print the table of 2 using for loop in c programing?

void main() { int i; char a[]="\0"; if(printf("%s\n",a)) printf("Ok here \n"); else printf("Forget it\n"); }

void main() { char far *farther,*farthest; printf("%d..%d",sizeof(farther),sizeof(farthest)); }

main() { int i=_l_abc(10); printf("%d\n",--i); } int _l_abc(int i) { return(i++); }

main() { int i=-1; +i; printf("i = %d, +i = %d \n",i,+i); }

write a c-program to find gcd using recursive functions

main() { int *j; { int i=10; j=&i; } printf("%d",*j); }

void main() { int i=i++,j=j++,k=k++; printf(“%d%d%d”,i,j,k); }

Write a Program that Inputs 10 Numbers in an Array and Show the Maximum Number

&#8206;#define good bad main() { int good=1; int bad=0; printf ("good is:%d",good); }

Categories
• C Code (421)
• C++ Code (127)
• VC++ Code (17)
• Java Code (84)
• Dot Net Code (114)
• Visual Basic Code (11)
• Programming Code AllOther (56)