1.

Write a program to check whether a given number is power of 2 using while statement.

Answer»

#include<iostream.h>

#include<iomainip.h>

void main()

{

int n,temp,flag = 1;

cout<<"Enter a number:";

cin>>n;

temp = n;

while(temp > 2)

{

If(temp % 2==1)

{

flag = 0;

break;

}

else

temp = temp/2;

}

If(flag = = 1)

cout<<n<<"is power of 2"<<endl;

else

cout<<n<<"is not power of 2"<<endl;

}



Discussion

No Comment Found

Related InterviewSolutions