C program

  • 714 Views
  • Last Post 05 September 2018
JohnBuencuses posted this 05 September 2018

Guys i didn't know what is the logic behind this prob. So i think i must post this in this group hehe , i hope somebody can solve this , using C programming Create a program that will convert Decimal to Binary 16 bit WITHOUT using math.h

I hope somebody can help me. Tiaaa

admin posted this 05 September 2018

You can try

#include <stdio.h>

int main()
{
  unsigned int n, c, k;

  printf("Enter an integer in decimal number system\n");
  scanf("%d", &n);

  printf("%d in binary number system is:\n", n);

  for (c = 15; c >= 0; c--)
  {
    k = n >> c;

    if (k & 1)
      printf("1");
    else
      printf("0");
  }

  printf("\n"); 
  return 0;
}

or

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

void binary(unsigned int);

void main()
{
   unsigned int number;
   printf("Enter Decimal Number: ");
   scanf("%u",&number);
   binary(number);
   getch();
}

void binary(unsigned int number)
{
   unsigned int mask=32768;//mask = [1000 0000 0000 0000]
   printf("Binary: ");

   while(mask > 0)
   {
      if((number & mask) == 0 )
         printf("0");
      else
         printf("1");
      mask = mask >> 1 ; // Right Shift
   }
}

As you know, An integer number can be represented by 16 bits. To convert the decimal number into binary, you should check first MSB bit of number, if it is 1 then display '1' otherwise display '0'.

I hope you can solve your problem

Close