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

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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

how to create duplicate link list using C???

2091


‘SAVEPOINT’ and ‘ROLLBACK’ is used in oracle database to secure the data comment. Give suitable examples of each with sql command.

1891


How to explain the final year project as a fresher please answer with sample project

488


Which are low level languages?

658


Why doesnt this code work?

632






write a program in c language to print your bio-data on the screen by using functions.

6263


Do pointers take up memory?

687


Why main is not a keyword in c?

664


What are the types of data types and explain?

681


What is masking?

660


What is the use of define in c?

614


What is structure padding in c?

643


Define VARIABLE?

703


What is a volatile keyword in c?

655


Write a program on swapping (100, 50)

654