Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


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

Why flag is used in c?

0 Answers  


#include<stdio.h> main() { char s1[]="Ramco"; char s2[]="Systems"; s1=s2; printf("%s",s1); } what will happen if you executed this code?

4 Answers   Ramco,


Is there something we can do in C but not in C++? Declare variable names that are keywords in C++ but not C.

2 Answers   Infosys,


What does #pragma once mean?

0 Answers   Celstream,


What is multidimensional arrays

0 Answers  


What is a const pointer?

0 Answers  


Can you please explain the difference between exit() and _exit() function?

0 Answers  


What is an auto variable in c?

0 Answers  


what is the output of following question? void main() { int i=0,a[3]; a[i]=i++; printf("%d",a[i] }

3 Answers  


write a program to check whether a given integer is a strong number or not? [Hint: 145=1!+4!+5! =1+24+120 =145]

7 Answers  


write a programme to enter some number and find which number is maximum and which number is minimum from enterd numbers.

3 Answers  


how write a addtion of two single dimensional array using of pointer in c language?

3 Answers   DRDO,


Categories