Sunday, 15 March 2015

To convert a decimal number to a binary number Using C

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

void main()
{
long b[20], n, r, c = 0, i ;
clrscr() ;
printf("Enter a decimal number : ") ;
scanf("%ld", &n) ;
while(n > 0)
{
r = n % 2 ;
b[c] = r ;
n = n / 2 ;
c++ ;
}
printf("\nThe binary equivalent is : ");
for(i = c - 1 ; i >= 0 ; i--)
printf("%ld", b[i]) ;
getch() ;
}

RUN 1 :
~~~~~~~
Enter a decimal number : 12
The binary equivalent is : 1100

No comments:

Post a Comment