top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Convert a binary number into decimal number and vice versa using C?

+1 vote
722 views
Convert a binary number into decimal number and vice versa using C?
posted Mar 9, 2017 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

0 votes
 
Best answer

Binary to Decimal

int binary_to_decimal(int bin)
{
    if (bin == 0)
    {
        return 0;
    }
    else
    {
        return (bin % 10 + 2 * binary_to_decimal(bin/ 10));
    }
}

Decimal to Binary

int decimal_to_binary(int dec)
{
    if (dec == 0)
    {
        return 0;
    }
    else
    {
        return (dec % 2 + 10 * decimal_to_binary(dec / 2));
    }
}
answer Apr 18, 2017 by Salil Agrawal
0 votes

here is function to print binary from decimal,you should provably use array if you want to store that but haw will you now the number of element did not figured out yet

int bin(int nmb)
{
  int temp;

  while(nmb)
  {
    temp = nmb % 2;
    printf("%d\n",temp);
    nmb = nmb >> 1;
  }
}
answer Apr 17, 2017 by Leon Martinović
This will print the output in reverse i.e. say input is 4 then output should be 100 where as the above program will print 001. Let me try this...
thx i did not see that becose i was using value 65,can you make array for this using dinamic alocation
implementing in c
check my other answer where I used recursion...let me know if it does not work out.
link please or can you send message
Similar Questions
+3 votes

Decimal to Factorial
23(D) = 3210(F)

23 / 3! = 3 (5)
5 / 2! = 2 (1)
1 / 1! = 1 (0)
0 / 0! = 0 (0)

Factorial to Decimal
3210(F)=23(D),
3*3!+2*2!+1*1!+0*0! = 23(D)

...