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

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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is the difference between abs() and fabs() functions?

609


What are the types of i/o functions?

685


Can a file other than a .h file be included with #include?

688


Why #include is used in c language?

605


What is the difference between fread buffer() and fwrite buffer()?

676






What is #line in c?

565


In a header file whether functions are declared or defined?

631


What is c system32 taskhostw exe?

596


Where in memory are my variables stored?

639


Explain what are the advantages and disadvantages of a heap?

599


What is pointer to pointer in c?

636


What is a null pointer assignment error? What are bus errors, memory faults, and core dumps?

907


What is c basic?

603


Explain what is a program flowchart and explain how does it help in writing a program?

652


Difference between exit() and _exit() function?

659