In this program, you'll learn to convert Decimal number to Binary number system. Decimal to binary in C: We can convert any decimal number (base-10 (0 to 9)) into binary number(base-2 (0 or 1)) by c program. Decimal number is a base 10 number, there are total 10 digits between 0 to 9. Any combination of digits is decimal number such as 23, 445, 132, 0, 2 etc. Binary number is a base 2 number contains only 2 digits either 0 or 1. Any combination of 0 and 1 is binary number such as 1001, 101, 11111, 101010 etc.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int dec, i;
long bin = 0;
printf("Enter the decimal number: ");
scanf("%d", &dec);
while(dec > 0)
{
bin = (bin * 10) + dec % 2;
dec = dec / 2;
}
printf("Binary Equivalent is: %ld", bin);
return 0;
}
Enter the decimal number: 10
Binary Equivalent is: 101
Enter the decimal number: 85
Binary Equivalent is: 1010101