Subscribe

RSS Feed (xml)

Wednesday, July 29, 2009

Write a C program to Convert any Decimal number into Binary coded decimal

#include(stdio.h)
#include(conio.h)
int main( )
{
int a[20], n, ct=0, rem , i=0;
clrscr( );
printf("Enter the required decimal number:");
scanf("%d", &n);
printf("\nBinary Number is :");
while(n>=1)
{
rem= n%2;
a[i]= rem;
i++; // to increase the array location
ct++; // to count the no. of bits storing in an array
n=n/2;
}
for(i=ct ; i>=1; i-- )
printf("%3d", a[i] );
getch( );
return 0;
}

Output:
Enter the required decimal number: 5
Binary Number is : 1 0 1

1 comment: