Pages

Convert Decimal to Binary in C

We all know how to convert Decimal number to Binary number. This example shows you how to implement that logic into our program. We simply the follow the following steps to get our requirement:
  • get reminder by dividing 2
  • multiply each reminder by 10 according to its position and add
  • divide the number by 2 and get remaining number.

Source Code:

#include <stdio.h>
#include <conio.h>


void main()
{
    int dec, temp, bin = 0, i = 1, rem;
   printf("Convert Decimal number to Binary Number");
   printf("\n------------------------------------------\n");
   printf("Enter Number:");
   scanf("%d",&dec);
   temp = dec;
   do
   {
     
      rem = temp  %  2;
      bin += rem * i;
      i *= 10;
      temp = temp / 2;
   }while(temp != 0);

   printf("Decimal Value = %d, Binary = %d", dec, bin);
   getch();
}

Output:

@msucil

Phasellus facilisis convallis metus, ut imperdiet augue auctor nec. Duis at velit id augue lobortis porta. Sed varius, enim accumsan aliquam tincidunt, tortor urna vulputate quam, eget finibus urna est in augue.

No comments:

Post a Comment