top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

WAP to find out is a given number is a power series of 2 or not, without any loop and without using % modulo operator.

0 votes
457 views
WAP to find out is a given number is a power series of 2 or not, without any loop and without using % modulo operator.
posted May 6, 2017 by Neeraj Kumar

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote
#include <stdio.h>

int main( )
{
   int n = 0;
   int input = 0;
   do
   {
      printf("Enter Number\n");
      scanf("%d", &n);
      if (!(n & n-1))
      {
         printf("Number is power of two\n");
      }
      printf("Do u want to continue. Press 0 to exit or 1 to continue\n");
      scanf("%d", &input);

   }
   while(input == 1);

   return 0;
}

In short, if output of number "n" & "n-1" results zero it means number "n" is power

answer May 7, 2017 by Vikram Singh
...