Write a C program to input decimal number from user and convert to binary number system. How to convert from decimal number system to binary number system in C.

Input any decimal number: 112

Output binary number: 0111000

Happy coding ;)

**Example:**Input any decimal number: 112

Output binary number: 0111000

### Required knowledge:

Basic C programming, For loop#### Decimal number system:

Decimal number system is a base 10 number system. Decimal number system uses only 10 symbols to represent all number i.e. 0 1 2 3 4 5 6 7 8 9#### Binary number system:

Binary number system is a base 2 number system. Binary number system uses only 2 symbols to represent all numbers i.e. 0 and 1### Algorithm to convert from Decimal to Binary number system:

Algorithm Decimal to Binary conversionbegin:read(DECIMAL);BINARY← 0;PLACE← 1;REM← 0;while(DECIMAL!=0) dobeginREM←DECIMAL% 2;BINARY← (REM*PLACE) +BINARY;PLACE←PLACE* 10;DECIMAL←DECIMAL/ 2;endwrite('Binary = 'BINARY)end

### Program:

/** * 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; /* * Reads decimal number from user */ printf("Enter any decimal number: "); scanf("%lld", &decimal); tempDecimal = decimal; /* * Converts the decimal number to binary number */ while(tempDecimal!=0) { rem = tempDecimal % 2; binary = (rem * place) + binary; tempDecimal /= 2; place *= 10; } printf("\nDecimal number = %lld\n", decimal); printf("Binary number = %lld", binary); return 0; }

**Note:**The above program converts the decimal to binary number only up to 18 binary bits. Use the below program to convert from decimal to binary number for a higher range.

### Program:

/** * 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; /* * Reads decimal number from user */ printf("Enter any decimal value : "); scanf("%ld", &decimal); /* Copies decimal value to temp variable */ tempDecimal = decimal; while(tempDecimal!=0) { /* Finds decimal%2 and adds to the binary value */ binary[index] = (tempDecimal % 2) + '0'; tempDecimal /= 2; index++; } binary[index] = '\0'; /* Reverse the binary value found */ strrev(binary); printf("\nDecimal value = %ld\n", decimal); printf("Binary value of decimal = %s", binary); return 0; }

### Program: Using bitwise operator

Check how to convert decimal number system to binary number system using bitwise operator.
Output

Enter any decimal value : 123

Decimal value = 123

Binary value of decimal = 1101111

Decimal value = 123

Binary value of decimal = 1101111

Happy coding ;)

### You may also like

- Loop programming exercises and solutions in C.
- C program to find one's complement of a binary number.
- C program to find two's complement of a binary number.
- C program to convert Binary to Octal number system.
- C program to convert Binary to Decimal number system.
- C program to convert Binary to Hexadecimal number system.
- C program to convert Octal to Binary number system.
- C program to convert Octal to Decimal number system.
- C program to convert Octal to Hexadecimal number system.
- C program to convert Decimal to Octal number system.
- C program to convert Decimal to Hexadecimal number system.
- C program to convert Hexadecimal to Binary number system.
- C program to convert Hexadecimal to Octal number system.
- C program to convert Hexadecimal to Decimal number system.

go recursive ...

ReplyDelete/* decimal to binary */

void decimal_to_binary(int a)

{

int lsb;

if(a == 0)

return;

lsb = a & 1;

decimal_to_binary(a>>1);

printf("%d", lsb);

}

Thanks @meh for the short recursive approach. I will update it the with same.

Delete