Canna

Monday, 24 September 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
  • Test Case 1

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

    Input (stdin)
    121
    
    
    Expected Output
    1111001
  • Program
  • #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;
    }

No comments:

Post a Comment

Note: only a member of this blog may post a comment.