Write a C program to convert decimal to binary number.
int main() {
int data, res = 0, i = 1, mod;
/* get the input from the user */
printf("Enter your input:");
scanf("%d", &data);
/* convert decimal to binary format */
while (data > 0) {
mod = data % 2;
res = res + (i * mod);
data = data / 2;
i = i * 10;
}
/* print the resultant binary value */
printf("Binary Value: %d\n", res);
return 0;
}
Output:
jp@jp-VirtualBox:~/$ ./a.out
Enter your input:255
Binary Value: 11111111
Enter your input:255
Binary Value: 11111111
No comments:
Post a Comment