Below is a small C program to convert a given decimal number to its binary representation. The code is very easy to understand and is commented out for better readability.
/*Program to convert the given number into binary format*/
main()
{
int num,tnum,bin[16],i,j,quot,rem;
clrscr();
/*Reading the decimal number from user*/
printf("Enter a decimal number:");
scanf("%d", &num);
/*Converting decimal number to binary*/
tnum = num;
i = 0;
while(num > 0)
{
bin[i] = num % 2;
num = num / 2;
i++;
}
/*Printing the binary number*/
printf("Binary representation of %d is: ",tnum);
for(j=i-1;j>=0;j--)
printf("%d",bin[j]);
getch();
}
Sample input and output:
Enter a decimal number:36
Binary representation of 36 is: 100100
No comments:
Post a Comment