Tuesday 12 September 2017

Decimal to binary

Convert a number to its binary.

Copy the code and paste it in here

#include <iostream>
#include<conio.h>
using namespace std;
void main()
{
int q, r, a, b[16] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
int i = 0;
cout << "\nEnter a number between -32768 and 32767....";
cin >> a;
if (a>0)
{
q = a;
}
else
{
q = -a;
b[15] = 1;
}
cout << "\nBinary in 16-bit format ";
for (i = 0; q != 1; i++)
{
r = q % 2;
b[i] = r;
q /= 2;
}
b[i] = 1;
for (int j = 0; j<16; j++)
{
if (j % 4 == 0)
cout << " ";
cout << b[15 - j];
}
_getch();
}

Comment below.





No comments:

Post a Comment