Example
Input
Input decimal number: 112
Output
Binary number: 0111000
Algorithm to convert from decimal to binary
Algorithm Decimal to Binary conversion begin: read (decimal); binary ← 0; place ← 1; rem ← 0; while (decimal > 0) do begin rem ← decimal % 2; binary ← (rem * place) + binary; place ← place * 10; decimal ← decimal / 2; end write('Binary = ' binary) end
/**
* C program to convert from Decimal to Binary number system
*/
#include <stdio.h>
int main()
{
long long decimal, tempDecimal, binary;
int rem, place = 1;
binary = 0;
/* Input decimal number from user */
printf("Enter any decimal number: ");
scanf("%lld", &decimal);
tempDecimal = decimal;
/* Decimal to binary conversion */
while(tempDecimal > 0)
{
rem = tempDecimal % 2;
binary = (rem * place) + binary;
tempDecimal /= 2;
place *= 10;
}
printf("Decimal number = %lld\n", decimal);
printf("Binary number = %lld", binary);
return 0;
}
Program to convert decimal to binary more than 18 bits
/**
* C program to convert from Decimal to Binary number system
*/
#include <stdio.h>
#include <string.h>
int main()
{
long decimal, tempDecimal;
char binary[65];
int index = 0;
/* Input decimal number from user */
printf("Enter any decimal value : ");
scanf("%ld", &decimal);
/* Copy decimal value to temp variable */
tempDecimal = decimal;
while(tempDecimal > 0)
{
binary[index] = (tempDecimal % 2) + '0';
tempDecimal /= 2;
index++;
}
binary[index] = '\0';
/* Reverse the converted binary to get final binary result */
strrev(binary);
printf("Decimal value = %ld\n", decimal);
printf("Binary value of decimal = %s", binary);
return 0;
}
/*
Output :
Enter any decimal value : 112
Decimal value = 112
Binary value of decimal = 01110000
*/