Function to find the given number is a power of 2 or not?

Answer Posted / sujan.t

/* 100% correct answer */

int n = 3;
boolean bool = true;
int reminder;
while (n >1)
{
reminder = n % 2;
if(reminder != 0)
{
bool = false;
break;
}
else
{
n = n / 2;
}
}
if (bool == true)
printf("The number is a power of two");
else
printf("The number is NOT A power of two");

Is This Answer Correct ?    1 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

How can you convert integers to binary or hexadecimal?

621


why wipro wase

1833


Define and explain about ! Operator?

617


Can I use base-2 constants (something like 0b101010)? Is there a printf format for binary?

581


What are the different types of constants?

644






What is array within structure?

589


Why static is used in c?

627


Write a program to know whether the input number is an armstrong number.

675


What is variable initialization and why is it important?

623


What is masking?

641


How can you determine the size of an allocated portion of memory?

748


What is the correct declaration of main?

681


You are to write your own versions of strcpy() and strlen (). Call them mystrcpy() and mystrlen(). Write them first as code within main(), not as functions, then, convert them to functions. You will pass two arrays to the function in the case of mystrcpy(), the source and target array.

1785


How does pointer work in c?

623


Explain about the functions strcat() and strcmp()?

601