Saturday, August 18, 2018

POWER OF 2

  • Problem Description

    Given a number , find whether it is a power of 2 or not

    NOTE There is a limit in Source code.
    Input

    The first Line contains T , the no of test cases followed by T lines.
    Each line has a integer X
    Output

    Output has T lines , with each line indicating whether the number is a power of 2 or not(print 1 if it a power of two else print 0).
  • CODING ARENA::
  • #include <stdio.h>
    #include<stdbool.h>
    bool isPowerOfTwo(int n)
    {
      if(n==0)
        return 0;
      while(n!=1)
      {
        if(n%2!=0)
          return 0;
        n=n/2;
      }
      return 1;
    }
          
    int main()
    {
      int a,i,b;
      scanf("%d",&a);
      for(i=0;i<a;i++)
      {
        scanf("%d",&b);
        isPowerOfTwo(b)?printf("1\n"):printf("0\n");
      }
      return 0;
    }

  • Test Case 1

    Input (stdin)
    4
    
    4
    
    0
    
    6
    
    8
    
    
    Expected Output
    1
    
    0
    
    0
    
    1
  • Test Case 2

    Input (stdin)
    2
    
    6
    
    8
    
    
    Expected Output
    0
    
    1

No comments:

Post a Comment