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

How can I invoke another program (a standalone executable, or an operating system command) from within a c program?

0 Answers  


What is string length in c?

0 Answers  


Who is invented by c?

24 Answers   Infosys, Mphasis,


can you change name of main()?how?

3 Answers   HCL, Siemens,


shorting algorithmS

0 Answers   Wipro,






The program will first compute the tax you owe based on your income. User is prompted to enter income. Program will compute the total amount of tax owed based on the following: Income Tax 0 - $45,000 = 0.15 x income $45,001 - $90,000 = 6750 + 0.20 x (income – 45000) $90,001 - $140,000 = 15750 + 0.26 x (income – 90000) $140,001 - $200,000 = 28750 + 0.29 x (income – 140000) Greater than $200,000 = 46150 + 0.33 x (income – 200000) Dollar amounts should be in dollars and cents (float point numbers with two decimals shown). Tax is displayed on the screen.

0 Answers   Microsoft,


What is quick sort in c?

0 Answers  


What are the advantages of Macro over function?

1 Answers  


How to add two numbers with using function?

2 Answers   Miracle Solutions,


Does * p ++ increment p or what it points to?

0 Answers  


Write a program in c to input a 5 digit number and print it in words.

11 Answers  


What is the value of h?

0 Answers  


Categories