Decimal to Binary

Algorithm

Input	Any integer number (up to 32767)
Output	Binary equivalent of input no.
Complexity	O(n)

Dec_to_Bin(A ,num)
1 Set i=0
2 while num>0
3	A[i]=num%2
4	num=num/2
5	i=i+1
6 for n=i-1 to 0
7	print “its binary number is A[n]

Algorithm Description

let us convert a decimal number 95 to binary number. Here the base of the binary number is 2 and the decimal number is 95. By following the above rule the quotient of 95 when we divide by 2 is 47 and remainder is 1. we proceed doing the similar operation. Divide the 47 by 2, the quotient is 23 and remainder is 1, again divide 23 by 2, the quotient is 11 and remainder is 1. We go on doing like this until the number becomes less then 2

C Code

//WAP to convert decimal to binary.

#include< stdio.h>
#include< conio.h>
void main()
{
	int i,num,a[20],n;
	clrscr();
	printf("Enter Decimal number:\n");
	scanf("%d",&num);
	i=0;
	while(num>0)
	{
		a[i]=num%2;
		num=num/2;
		i++;
	}
	printf("Its Binary number:\n");
	for(n=i-1;n >=0;n--)
	printf("%d",a[n]);
}