Convert Decimal to Binary

Decimal number contains 10 digits(0-9) but Binary number contains two digits(0,1).

 

//Program to convert decimal to binary

#include<stdio.h>

#include<conio.h>

long binary(long int num);

void main()

   {

                long int num;

                printf("Enter the decimal no.");

                scanf("%ld",&num);

                printf("Decimal=%ld, Binary=%ld\n",num,binary(num));

   }

long binary(long int num)

   {

                long rem,a=1,bin=0;

                while(num>0)

                 {

                  rem=num%2;

                  bin=bin+rem*a;

                  num/=2;

                  a*=10;

                 }

     return bin;

   }

Decimal Number…..Our present number system a base or radix od 10 is based on the use of ten different digits(0-9)for example 210.The highest numerical symbol    i.e 9 has a value one less than the base for the decimal number system.

Binary Number……Binary number system uses only 2 digits(bits), i.e 0,1 and has the base of 2.

Convert decimal to binary

About Us

Rekha SetiaRekha Setia is a passionate blogger of Extra Computer Notes. if you have any ideas or any request me @