Saturday, August 18, 2018

BINARY FROM DECIMAL


  • Problem Description

    Lydia learn number conversion concept in C. Her teacher gave a homework to convert the number from decimal to binary. Help her to convert the number from decimal to binary. 

    Input should be from 0 to 127

    64 32 16 8 4 2 1
  • CODING ARENA
  • #include <stdio.h>
    int main()
    {
      int n,c,k;
      scanf("%d",&n);
      for(c=6;c>=0;c--)
      {
        k=n>>c;
        if(k&1)
          printf("1");
        else
          printf("0");
      }
      return 0;
    }
      

  • Test Case 1

    Input (stdin)
    1
    
    
    Expected Output
    0000001
  • Test Case 2

    Input (stdin)
    121
    
    
    Expected Output
    1111001

No comments:

Post a Comment