Decimal to binary converter 0.0
Submitted By:
ronoholic
Rating:
(Not rated) (
Rate It)
//LOGIC OF THE PROGRAM
/*if a number >= 2^n its given 1 else 0. its similar to writing the following
for example: binary of 120 would be 1111000
decimal: 128 64 32 16 8 4 2 1
binary: 0 1 1 1 1 0 0 0
just add up the numbers which have 1 underneath them,it gives 120
all you need to do to find binary equivalent is dat make such a combo that
powers having 1 under them add to form the decimal number*/
#include<conio.h>
#include<math.h>
void main()
{
unsigned long i=0;
int m=0,comp,n=0,j=0;
clrscr();
printf("Enter the decimal number (upto 1024): ");
scanf("%d",&n);
for(m=10;m>=0;m--)
{
comp=pow(2,m); //calculates 2^n like 2,4,8,16...
if(n>=comp) //comparison with 2^n
{
j=1; //true so 1 is given
n=n-comp; //subtracting as we are going down
} //the order 128->64->32....
else j=0;
i=10*i+j; //generating the binary number
}
printf("\nThe decimal equivalent is: %ld",i);
getch();
}