Decimal to Binary Converter

//convert a decimal number to a binary representation.

#include <iostream>
#include <stdlib.h>


using namespace std;


char *printBin(int n)
{
	static char binString[17];
	int i;
	
	for(i=0; i<16; i++)
	{
		binString[15-i] = n & 1 ? '1': '0';
		n = n/2;
	}
	return binString;
}
	


int main(int argc, char*argv[])
{

if(argc<2)
{
cerr<<"Usage dectobin [decimal number]"<<endl;
return 1;
}
	int a = atoi(argv[1]);
	cout<<printBin(a)<<endl;


return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *