Decimal to binary equivalent in c.
code:
#include <stdio.h>
int main()
{
int dn,i;
scanf("%d",&dn);
for(i=1;i<=dn;i*=2)
{}
for(i/=2;i!=1;i/=2)
{
printf("%d",dn/i);
dn=dn%i;
}
printf("%d",dn);
return 0;
}
Discussion:
In this program it use the easies formula to convert decimal to binary.
The formula is:
n ……. 64 32 16 8 4 2 1
calculate the nearby value of the given decimal number in this series.When the
decimal value are first time divided by the series value it will be started count. Then
print the division result and calculate the modulus for these number and assigned in it.
For Example:
where decimal number is 17
step 1: Nearby value of this number on the series.
step 2: Print the division result.
step 3: Assign modulus in it. And take next number on the series.
step 4: check the value of this series is not 1. Then go to step 2.Other wise print the last value.
step 5: End
so here
dn=17 , i=16 print 1 and get dn=1 , I=8.
print 0 and get dn=1 , I = 4.
print 0 and get dn = 1 , I = 2.
print 0 and get dn = 1 , I = 1
print 1 because I = 1 it never be back
#include <stdio.h>
int main()
{
int dn,i;
scanf("%d",&dn);
for(i=1;i<=dn;i*=2)
{}
for(i/=2;i!=1;i/=2)
{
printf("%d",dn/i);
dn=dn%i;
}
printf("%d",dn);
return 0;
}
Discussion:
In this program it use the easies formula to convert decimal to binary.
The formula is:
n ……. 64 32 16 8 4 2 1
calculate the nearby value of the given decimal number in this series.When the
decimal value are first time divided by the series value it will be started count. Then
print the division result and calculate the modulus for these number and assigned in it.
For Example:
where decimal number is 17
step 1: Nearby value of this number on the series.
step 2: Print the division result.
step 3: Assign modulus in it. And take next number on the series.
step 4: check the value of this series is not 1. Then go to step 2.Other wise print the last value.
step 5: End
so here
dn=17 , i=16 print 1 and get dn=1 , I=8.
print 0 and get dn=1 , I = 4.
print 0 and get dn = 1 , I = 2.
print 0 and get dn = 1 , I = 1
print 1 because I = 1 it never be back
মন্তব্যসমূহ
একটি মন্তব্য পোস্ট করুন