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

Answers were Sorted based on User's Feedback



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

Answer / abhinay

main()
{
int n,m;
do
{
m=n%2;
n=m;
}
while(n>1);
if(m==1)
{
Printf("Number is not a power of 2");
}
else
{
printf("Number is a power of 2");
}
}

Is This Answer Correct ?    1 Yes 0 No

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

Answer / sagar bhagat

#include<stdio.h>
#include<conio.h>
void main()
{
int num,i,flag=0,temp;
clrscr();

printf("\nEnter the number=");
scanf("%d",&num);

if(num<=0)
printf("Plz enter anoter num");
else
{
temp=num;
for(i=1;(i<num) ;i++)
{
if((i*i)==temp)
{
flag=1;
break;
}
}
}
if(flag==1)
printf("\nNumber %d is in power of 2 of %d",num,i);
else
printf("\nNumber %d is not in power of 2 ",num);

getch();
}

Is This Answer Correct ?    1 Yes 0 No

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

Answer / 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

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

Answer / anyone

//Revision for Ahmed Ihmeid answer:
//Operator % prototype: int % int

bool isPowerOf2(int num)
{
int x;

x = num % (int(ceil(sqrt(num)));

if( x == 0)
return true; //it is a power of 2
else
return false; //it is not
}

proof:
4 % 2 = 0
16 % 4 = 0
22 % 5 != 0

Is This Answer Correct ?    2 Yes 2 No

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

Answer / 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

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

Answer / ahmed ihmeid

bool isPowerOf2(int num)
{
float x;

x = num % (sqrt(num));

if( x == 0)
return true; //it is a power of 2
else
return false; //it is not
}

proof:
4 % 2 = 0
16 % 4 = 0
22 % 4.7 != 0

Is This Answer Correct ?    1 Yes 2 No

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

Answer / hassan noureddine

int isPowrOf2 (unsigned int number)
{
float x;

if (number <= 0) return (0);

x = log(number) / log(2);
return ( (int) ( ((int)x*log(2))/log(number)));
}

Is This Answer Correct ?    4 Yes 6 No

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

Answer / sudhesh

#include<stdio.h>
main()
{
int num,result;
printf("\nEnter the number:=");
scanf("%d",&num);
if (result=num&(num-1))
printf("No\n");
else
printf("Yes\n");
}

Is This Answer Correct ?    1 Yes 3 No

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

Answer / jessu srikanth

int isPowerOf2(unsigned int n)
{
float r=n;
while(r>1) r/=2.0;
return (r==1)?1:0; /* 1-> TRUE; 0-> FALSE*/
}

Is This Answer Correct ?    9 Yes 12 No

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

Answer / sheikh rasel

#include<stdio.h>
main()
{
int i;
if(i%2=0)
printf("The given number is a power of 2");
else
printf("The given number is not a power of 2");
return 0;
}

Is This Answer Correct ?    6 Yes 133 No

Post New Answer

More C Interview Questions

What will the code below print when it is executed?   int x = 3, y = 4;         if (x = 4)                 y = 5;         else                 y = 2;         printf ("x=%d, y=%d ",x,y);

0 Answers  


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

0 Answers  


sir, i cannot find the way how to write aprogram by using array on queue

1 Answers   IISIT,


What is the heap in c?

0 Answers  


What is meant by global static? why we have to use static variable instead of Global variable

4 Answers   L&T,






void swap(int a,int b) { a=a+b; b=a-b; a=a-b; } in this code always gives the same result for all case

9 Answers   Accenture, TCS,


7. Identify the correct argument for the function call fflush() in ANSI C: A)stdout B)stdin C)stderr D)All the above

10 Answers   Accenture,


What is c++ used for today?

0 Answers  


There are 3 baskets of fruits with worng lables,one basket has apple,another basket has orange,another has combination of apple and orange,what is the least way of interchange the lables.

15 Answers   Cisco, Google, MBT,


get any number as input except 1 and the output will be 1.without using operators,expressions,array,structure.don't print 1 in printf statement

3 Answers  


Why is c faster?

0 Answers  


Explain spaghetti programming?

0 Answers  


Categories