-->

program to convert decimal number into binary.

//program to convert decimal number into binary.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int k=1,m=0,n,rem,binary=0;
printf("enter decimal value/number of 'n'\n");
scanf("%d",&n);
while(n!=0)
   {
      rem=n%2;
      binary=binary+rem*pow(10,m);
      n=n/2;
      m++;
   }
printf("binary is=%d\n",binary);
getch();
}



logics in mind:

if decimal number is 9 (base 10) then
binary=dividing 9  by 2 and getting remainder.It is 1001. 
making its formula in program
1001=1000+000+00+1
         =1*103+0*102+0*101+1*100
       note:

it does not work for long values(more than 30 04 28). It can, if you declare variables as long. 

No comments:

Post a Comment