#include <stdio.h>
int main() {
int decimal, binary = 0, place = 1;
printf("Enter a decimal number: ");
scanf("%d", &decimal);
while (decimal != 0) {
int digit = decimal % 2;
binary += digit * place;
decimal /= 2;
place *= 10;
}
printf("The binary equivalent is %d\n", binary);
return 0;
}
Enter a decimal number: 234
The binary equivalent is 11101010
decimal
number, binary to store the binary
equivalent, and place to keep track of the current place
value (1, 10, 100, etc.).printf
and scanf
.while
loop to convert the decimal number to binary. The loop continues until the decimal number becomes 0.%
, which gives us the rightmost binary digit. We multiply this digit with the current place value and add it to the binary
variable.binary digit
, and multiply the place variable by 10 to move to the next place value (10, 100, 1000, etc.
).binary
variable as the binary equivalent using printf
.