Solved programming problem

Check if The Number is A power of 2 in C.

Source code

#include<stdio.h>

#include<math.h>

int main()

{

    int n;

    double count=0;

    printf("Please Enter Any number: ");

    scanf("%d",&n);

    int m=n;

    while(n%2==0 && n>1)

    {

        count++;

        n=n/2;

    }

    if(m==pow(2,count))

    {

        printf("
It is a Power of 2.
");

    }

    else

    {

        printf("
It is Not a Power of 2.
");

    }

    return 0;

}

Output

Please Enter Any number: 512

It is a Power of 2.

 

Join the discussion

Add your comment