### C Code Snippets 5 - Decimal to Binary Conversion

Decimal number system is the most commonly used and the most familiar one to the general public. It is also known as Base 10 numbering system since it is based on 10 following symbols: 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9.

Binary is the simplest kind of number system that uses only two digits of 0 and 1. By using these digits, computational problems can be solved by machines because in digital electronics a transistor is used in two states. Those two states can be represented by 0 and 1.

That is why this number system is the most preferred by modern computer engineers, networking and communication specialists, and other professionals.

(7)10 = (111)2

(17)10 = (10001)2

(51)10 = (110011)2

(217)10 = (11011001)2

Our solution converts a decimal value to its binary equivalent by using a recursive convert function. Please leave a comment if you have any questions or comments. Thanks !

``````
/**
* Author : Berk Soysal
*/

#include <stdio.h>

//Function Prototype
int convert(int);

int main()
{
int dec, bin;

printf("Enter a decimal number: ");
scanf("%d", &dec);

bin = convert(dec);
printf("The binary equivalent of %d is %d.\n", dec, bin);

return 0;
}

//Convert Function
int convert(int dec)

{
if (dec == 0)
{
return 0;
}
else
{
return (dec % 2 + 10 * convert(dec / 2));
}
}

``````
Result
``` ```
` `
`Keywords: CCode Snippets `