ALLInterview.com :: Home Page KalAajKal.com
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage  Contact Us     Login  |  Sign Up                      
tip   To Refer this Site to Your Friends   Click Here
Google
 
Categories  >>  Code Snippets  >>  Programming Code  >>  C Code
 
 


 

 
 C Code interview questions  C Code Interview Questions
 C++ Code interview questions  C++ Code Interview Questions
 VC++ Code interview questions  VC++ Code Interview Questions
 Java Code interview questions  Java Code Interview Questions
 Dot Net Code interview questions  Dot Net Code Interview Questions
 Visual Basic Code interview questions  Visual Basic Code Interview Questions
 Programming Code AllOther interview questions  Programming Code AllOther Interview Questions
Question
Give a one-line C expression to test whether a number is a
power of 2.
 Question Submitted By :: =-PKG-=
I also faced this Question!!     Rank Answer Posted By  
 
  Re: Give a one-line C expression to test whether a number is a power of 2.
Answer
# 1
#include<iostream.h>
int main()
{
        int n,i;
        cout<<"Enter a number";
        cin>>n;
        for(i=1;i<n/2;i++)
        {
                if((2<<i)==n)
                {
                        cout<<"The given no is power of 2";
                        break;
                }
        }
}
 
Is This Answer Correct ?    3 Yes 3 No
Santhoo035
[Nan]
 
  Re: Give a one-line C expression to test whether a number is a power of 2.
Answer
# 2
#include<stdio.h>
void main()
{
	int i,j;
	clrscr();
	printf("\n Enter a num");
	scanf("%d",&i);
	j=i;
	for(;i%2==0;i/=2);
	if(i==1)
	printf("\n%d is power of 2",j);
	else
	printf("\n%d is not a power of 2");
	getch();
}
 
Is This Answer Correct ?    1 Yes 9 No
Trailokya Ranjan Jena
 
 
 
  Re: Give a one-line C expression to test whether a number is a power of 2.
Answer
# 3
void main()
{
int a;
scanf("%d",&a);

if((a&a-1)==0)
printf("Is a power of 2");
else
printf("Not a power of 2");
}
 
Is This Answer Correct ?    7 Yes 2 No
Lakshmi
 
  Re: Give a one-line C expression to test whether a number is a power of 2.
Answer
# 4
#include<conio.h>
void main()
{
  int n,i=0,num=0;
  printf("\n enter any number");
  scanf("%d",&n);
  while(num<=n)
   {
     i++;
     num=2^i;
     }
if(num==n)
printf("yes no. is power of 2");
else
printf("no.");
}
 
Is This Answer Correct ?    0 Yes 2 No
Akshay Rastogi
 
  Re: Give a one-line C expression to test whether a number is a power of 2.
Answer
# 5
/*following expr evaluates to true if num is a power of 
2.Else it's false. '&' - bitwise and.*/
             (num == 1) || !(num & 1)
 
Is This Answer Correct ?    0 Yes 6 No
Abc Def
 
  Re: Give a one-line C expression to test whether a number is a power of 2.
Answer
# 6
if(x&(x-1)==0)
  then TRUE;
else
  FALSE;

use the bitwise and operator
 
Is This Answer Correct ?    8 Yes 2 No
Fallen Angel
 
  Re: Give a one-line C expression to test whether a number is a power of 2.
Answer
# 7
if((log(n)/log(2))/floor(log(n)/log(2))==1)
then TRUE;
else FALSE;
 
Is This Answer Correct ?    1 Yes 2 No
Rohit
 
  Re: Give a one-line C expression to test whether a number is a power of 2.
Answer
# 8
#include<stdio.h>
main()
{
 int n,i,d,m;
 printf("Enter a number");
 scanf("%d",&n);
 m=n;
 while(n>0)
 {
  d=n%10;
  if(d!=0)
  {
   prntf("%d is not power of 2",m);
   getch();
   exit();
  }
   n=n/10;
 }
  prntf("%d is power of 2",m);
  getch();
}
 
Is This Answer Correct ?    0 Yes 0 No
Ramesh B Penchal
 

 
 
 
Other C Code Interview Questions
 
  Question Asked @ Answers
 
#include"math.h" void main() { printf("Hi everybody"); } if <stdio.h> will be included then this program will must compile, but as we know that when we include a header file in "" then any system defined function find its defination from all the directrives. So is this code of segment will compile? If no then why?  2
int aaa() {printf(“Hi”);} int bbb(){printf(“hello”);} iny ccc(){printf(“bye”);} main() { int ( * ptr[3]) (); ptr[0] = aaa; ptr[1] = bbb; ptr[2] =ccc; ptr[2](); }  1
Write a function to find the depth of a binary tree. Adobe8
void main() { int i=5; printf("%d",i++ + ++i); }  1
#if something == 0 int some=0; #endif main() { int thing = 0; printf("%d %d\n", some ,thing); }  1
main() { extern i; printf("%d\n",i); { int i=20; printf("%d\n",i); } }  1
char *someFun1() { char temp[ ] = “string"; return temp; } char *someFun2() { char temp[ ] = {‘s’, ‘t’,’r’,’i’,’n’,’g’}; return temp; } int main() { puts(someFun1()); puts(someFun2()); }  1
How will u find whether a linked list has a loop or not? Microsoft6
Link list in reverse order. NetApp7
program to Reverse a linked list Ness-Technologies4
void main() { int const * p=5; printf("%d",++(*p)); }  1
void main() { unsigned giveit=-1; int gotit; printf("%u ",++giveit); printf("%u \n",gotit=--giveit); }  1
main() { char *p; printf("%d %d ",sizeof(*p),sizeof(p)); }  1
Given an array of characters which form a sentence of words, give an efficient algorithm to reverse the order of the words (not characters) in it. Microsoft7
#ifdef something int some=0; #endif main() { int thing = 0; printf("%d %d\n", some ,thing); }  1
#include<stdio.h> main() { struct xx { int x=3; char name[]="hello"; }; struct xx *s; printf("%d",s->x); printf("%s",s->name); }  1
main() { if ((1||0) && (0||1)) { printf("OK I am done."); } else { printf("OK I am gone."); } } a. OK I am done b. OK I am gone c. compile error d. none of the above HCL1
main() { char i=0; for(;i>=0;i++) ; printf("%d\n",i); }  1
1 o 1 1 0 1 0 1 0 1 1 0 1 0 1 how to design this function format in c-language ?  1
main() { int a=2,*f1,*f2; f1=f2=&a; *f2+=*f2+=a+=2.5; printf("\n%d %d %d",a,*f1,*f2); }  1
 
For more C Code Interview Questions Click Here 
 
 
 
 
 
   
Copyright Policy  |  Terms of Service  |  Help  |  Site Map 1  |  Articles  |  Site Map  |   Site Map  |  Contact Us interview questions urls   External Links 
   
Copyright © 2007  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  Forum9.com   ::  KalAajKal.com