Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5

Write a Program to check if an integer is a power of 2 or not?
#1

Write a Program to check if an integer is a power of 2 or not?
Reply
#2

Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int num,r,temp,flag= -1;
clrscr();
printf("Enter any integer\n");
scanf("%d",&num);
temp=num;
while(temp>2)
{
r=temp%2;
if(r==1)
{
flag=0;
break;
}
else
temp=temp/2;
}
if(flag==0)
printf("%d is not the Power of 2",num);
else
printf("%d is power of 2",num);
getch();
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)