Given an unsigned integer, find if the number is power of 2?

Answers were Sorted based on User's Feedback



Given an unsigned integer, find if the number is power of 2?..

Answer / veerendra jonnalagadda

main()
{
int i;
printf("Enter Number :");
scanf("%d",&i);
if(i&(i-1))
printf("Not atwo power");
else
printf("Two 's Power");
}

Is This Answer Correct ?    5 Yes 0 No

Given an unsigned integer, find if the number is power of 2?..

Answer / veerendra jonnalagadda

main()
{
int i;
printf("Enter Number :");
scanf("%d",&i);
if(i&(i-1))
printf("Not atwo power");
else
printf("Two 's Power");
}

Is This Answer Correct ?    3 Yes 0 No

Given an unsigned integer, find if the number is power of 2?..

Answer / coder

#include<stdio.h>
void powerOfTwo(int number)
{
if(!(number & number-1) && number)
printf("\nthe number is a power of 2\n");
else printf("\nThe number is not a power of 2\n");
}


int main()
{
powerOfTwo(32); //power of 2
powerOfTwo(22); //not a power of 2
return 0;
}

Is This Answer Correct ?    2 Yes 1 No

Given an unsigned integer, find if the number is power of 2?..

Answer / asis bera

main()
{
unsigned int n;
printf("enter the number:\n");
scanf("%u",&n);
if(i&(i-1))
printf(" not power of two\n");
else
printf("power of two...\n");
}

Is This Answer Correct ?    0 Yes 1 No

Given an unsigned integer, find if the number is power of 2?..

Answer / sagar shah

#include<stdio.h>
main()
{
int i,n,r=2
clrscr();
scanf("%d",&n);
for(i=1;i<=n;i=r*i)
{
if(i==n)
{
r=0;
break;
}
}
if (r==0)
{
printf("power of two:");
}
else
{
printf("not power of two:");
}
getch();
}

Is This Answer Correct ?    0 Yes 1 No

Post New Answer

More C Interview Questions

What are the types of unary operators?

0 Answers  


Write a C program that will accept a hexadecimal number as input and then display a menu that will permit any of the following operations to be carried out: Display the hexadecimal equivalent of the one's complement. (b) Carry out a masking operation and then display the hexadecimal equivalent of the result. (c) Carry out a bit shifting operation and then display the hexadecimal equivalent of the result. (d) Exit. If the masking operation is selected, prompt the user lor the type of operation (bitwise and, bitwise exclusive or, or bitwise or) and then a (hexadecimal) value for the mask. If the bit shifting operation is selected. prompt the user for the type of shift (left or right), and then the number of bits. Test the program with several different (hexadecimal) input values of your own choice.

0 Answers  


main() { int i = -3,j=2,k=0,m; m= ++i || ++j && ++k; printf("%d%d%d",i,j,k,m); }

7 Answers   HCL,


void main() { int i=5; printf("%d",i+++++i); }

17 Answers   ME,


If errno contains a nonzero number, is there an error?

0 Answers  






Is it better to use a macro or a function?

0 Answers  


Explain spaghetti programming?

0 Answers  


which type of aspect you want from the student.

0 Answers   IBM, TCS,


what is diff b/w huge & far & near pointer??

1 Answers   HCL,


In scanf h is used for

4 Answers   BFL,


how to swap two nubers by using a function with pointers?

1 Answers  


change to postfix a/(b+c*d-e)

8 Answers   Value Labs,


Categories