
-----------------------------------
alpesh
Sat Mar 10, 2007 7:18 am

Decimal to Binary
-----------------------------------
#include
#include
#include
void main()
{
	 int n,r,s=0,i=0;
	clrscr();
	printf("Please enter decimal number");
	scanf("%d",&n);
	while(n>0)
	{
		r=n%2;
		s=s+r*pow(10,i);
		n=n/2;
		i=i+1;
	}
	printf("%d",s);
	getch();
}

-----------------------------------
GiffordBruno
Tue May 25, 2010 8:32 am

RE:Decimal to Binary
-----------------------------------
Very nice result. Thanks for this great information.

-----------------------------------
A.J
Tue May 25, 2010 10:36 am

RE:Decimal to Binary
-----------------------------------
If you want, you could code you own 'bitset' function for C. Basically, bitset(d) outputs 'd' into binary, showing 'x' bits (so if you output bitset(5), it outputs 0000000101). Though the problem with that is it only converts to binary.

Good job though.

-----------------------------------
Prabhakar Ragde
Tue May 25, 2010 10:56 am

RE:Decimal to Binary
-----------------------------------
Two possible improvements:

1) Eliminate the use of the `pow' function, which isn't needed.

2) Write a version which works for all integers n that can be represented as C ints. Currently this program only works for small nonnegative integers.

-----------------------------------
coolgod
Sat Aug 07, 2010 2:16 am

Re: Decimal to Binary
-----------------------------------
a better one would be

#include 

int main()
{
	int n;
	printf("Please input the integer you want to convert to binary:");
	scanf("%d", &n);
	int i;
	for(i=0; i