Program to convert decimal to binary in c




Decimal to binary conversion in c programming language. C source code for decimal to binary conversion:


#include<stdio.h>


int main(){

    long int decimalNumber,remainder,quotient;

    int binaryNumber[100],i=1,j;


    printf("Enter any decimal number: ");

    scanf("%ld",&decimalNumber);


    quotient = decimalNumber;


    while(quotient!=0){

         binaryNumber[i++]= quotient % 2;

         quotient = quotient / 2;

    }


    printf("Equivalent binary value of decimal number %d: ",decimalNumber);

    for(j = i -1 ;j> 0;j--)

         printf("%d",binaryNumber[j]);


    return 0;

}

Sample output:

Enter any decimal number: 50
Equivalent binary value of decimal number 50: 110010



Algorithm:


Binary number system: It is base 2 number system which uses the digits from 0 and 1.

Decimal number system:
It is base 10 number system which uses the digits from 0 to 9

Convert from decimal to binary algorithm:

Following steps describe how to convert decimal to binary

Step 1: Divide the original decimal number by 2
Step 2: Divide the quotient by 2
Step 3: Repeat the step 2 until we get quotient equal to zero.

Equivalent binary number would be remainders of each step in the reverse order.

Decimal to binary conversion with example:

For example we want to convert decimal number 25 in the binary.

Step 1:  25 / 2  Remainder : 1 , Quotient : 12
Step 2:  12 / 2  Remainder : 0 , Quotient : 6
Step 3:   6 / 2  Remainder : 0 , Quotient : 3
Step 4:   3 / 2  Remainder : 1 , Quotient : 1
Step 5:   1 / 2  Remainder : 1 , Quotient : 0

So equivalent binary number is: 11001
That is (25)10 = (11001)2





6. Write a c program to convert octal number to hexadecimal number.
8. Write a c program to convert hexadecimal number to octal number.
9. Write a c program to convert hexadecimal number to decimal number.
10. Write a c program to convert binary number to octal number.

29 comments:

  1. thanks to ur great effort........ur work has really been helpful to me n will always be grateful to u.....
    thanks!
    aayush.

    ReplyDelete
  2. truly i feel great to come here

    ReplyDelete
  3. I see your algorithm logic for this uses value 'remainder' and your code declares a variable long int remainder; but this is unused in the program? I'm not sure why!

    ReplyDelete
  4. Yes you are correct. No need to declare remainder. Thanks

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. What about negative decimal numbers as input, will it work?

    ReplyDelete
  7. what if the number has a fractional part?

    ReplyDelete
    Replies
    1. wtf its actually a good question.

      Delete
    2. its simple we can just take decimal part and multiply by 2. take reminder again multiply by 2. Repeat until we get 0

      Delete
  8. This comment has been removed by the author.

    ReplyDelete
  9. how can i save the new binary number in a new array instead of printing it?
    thanks in advance

    ReplyDelete
  10. thankx for your help your solution was really very much helpful. i really felt great. Thankx

    ReplyDelete
  11. how can you convert a decimal array into binary one using recursion?
    example:
    (decimal) 1,2,3,4,5 => (binary)0001,0010, 0011, 0100, 0101

    ReplyDelete
    Replies
    1. dude,send me the answer if u have got it.

      Delete
  12. plz answer to olga kuzmin's question..

    ReplyDelete
  13. thanks a lot sir! it helps me a lot!

    ReplyDelete
  14. What if the decimal number is 0? It won't enter the loop

    ReplyDelete
  15. @olga====apply the conversion code in a for loop that trace throufh the array

    ReplyDelete
  16. There is one 0 (zero) more than should be,in the code(last FOR):

    for(j = i -1 ;j> 0;j--)

    there should be J>1 not 0.

    ReplyDelete
  17. were should i put the clrscr(); ??? thanks

    ReplyDelete
  18. Hello dear!!,
    you here printing one by one number. But when you should print it as a number!! Suppose, you need to use the binary conversion as a number,then your logic does not work.
    Thanks.

    ReplyDelete
  19. megha agrawal12/13/13, 5:20 PM

    #include
    #include
    int main()
    { long int decimal,binr=0;
    int bin=0,rem,i;
    printf("enter the decimal number:");
    scanf("%ld",&decimal);
    for(i=1;decimal!=0;i++)
    { rem=decimal%2;
    binr= binr*10 +rem;
    decimal=decimal/2;

    }
    for(i=0;binr!=0;i++)
    { rem = binr%10;
    bin= bin*10 +rem ;
    binr=binr/10;
    }
    printf("the binary equivalent is: %ld",bin);
    getch();
    return 0;
    }
    This is a code with0ut using a array and can be used for decimal to octal num also anly the change is to make 8, where 2 is written.

    ReplyDelete
  20. #include
    void binary(int );
    int main()
    {
    int n;
    scanf("%d",&n);
    binary(n);
    return 0;
    }
    void binary(int p)
    {
    if(p==1)
    printf("1");
    else
    {
    binary(p/2);
    printf("%d",p%2);
    }
    }

    ReplyDelete
  21. Hey, quick question about the value of i. Should the value of i be 100 and not 1. If it 1, the for loop with j would give an error? I'm confused about that.

    ReplyDelete
    Replies
    1. Nevermind. I got the point! Thanks!

      Delete
  22. how to write a program to print the boundary of a matrix

    ReplyDelete
  23. why do i get this and each number of the binary is in a different line and how to fix it
    for instance the number 4 shows as
    1
    0
    0

    ReplyDelete
  24. This comment has been removed by the author.

    ReplyDelete