Write a program to convert ASCII representation of Decimal number
(loaded from the keyboard using a loop calling the function getchar() )
and print its representation in Binary number system
(example: 13 » 00001101 )
#include <stdio.h>
#include <math.h>
int main()
{
int binary[100], dec, j = 7;
printf("Enter a character: ");
scanf("%d",&dec);
printf("\nThe ascii value of the ch variable is : %d\n", dec);
while ( dec > 0 ) {
binary[j] = dec % 2;
dec /= 2;
--j;
}
while ( j >= 0 ) { // Pad with leading 0s.
binary[j] = 0;
--j;
}
printf("\nBinary number is: ");
while ( ++j < 8 ) {
printf("%d",binary[j]);
}
printf("\n");
getchar();
getchar();
return 0;
}
Comments
Leave a comment