This blog is under construction

Monday 24 June 2013

C program to convert decimal to binary

Write a C program to convert decimal to binary number.


  #include <stdio.h>
  int main() {
        int data, res = 0, i = 1, mod;

        /* get the input from the user */
        printf("Enter your input:");
        scanf("%d", &data);

        /* convert decimal to binary format */
        while (data > 0) {
                mod = data % 2;
                res = res + (i * mod);
                data = data / 2;
                i = i * 10;
        }

        /* print the resultant binary value */
        printf("Binary Value: %d\n", res);
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter your input:255
  Binary Value: 11111111



No comments:

Post a Comment