# C Program to Convert Decimal Number to Binary Number

0 comment

In this tutorial, we will write a C Program to Convert Decimal Number to Binary Number. To convert decimal number to binary number divide a decimal number by two, if the quotient is not zero again divided it by two and keep dividing it by two until the quotient is equal to zero.

```.wp-block-code {
border: 0;
}

.wp-block-code > div {
overflow: auto;
}

.shcb-language {
border: 0;
clip: rect(1px, 1px, 1px, 1px);
-webkit-clip-path: inset(50%);
clip-path: inset(50%);
height: 1px;
margin: -1px;
overflow: hidden;
position: absolute;
width: 1px;
word-wrap: normal;
word-break: normal;
}

.hljs {
box-sizing: border-box;
}

.hljs.shcb-code-table {
display: table;
width: 100%;
}

.hljs.shcb-code-table > .shcb-loc {
color: inherit;
display: table-row;
width: 100%;
}

.hljs.shcb-code-table .shcb-loc > span {
display: table-cell;
}

.wp-block-code code.hljs:not(.shcb-wrap-lines) {
white-space: pre;
}

.wp-block-code code.hljs.shcb-wrap-lines {
white-space: pre-wrap;
}

.hljs.shcb-line-numbers {
border-spacing: 0;
counter-reset: line;
}

.hljs.shcb-line-numbers > .shcb-loc {
counter-increment: line;
}

.hljs.shcb-line-numbers .shcb-loc > span {
}

.hljs.shcb-line-numbers .shcb-loc::before {
border-right: 1px solid #ddd;
content: counter(line);
display: table-cell;
text-align: right;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
white-space: nowrap;
width: 1%;
}
```#include<stdio.h>

int main(){
int n = 4;
int rem;
int a[10];
int i=0;
while(n){
rem = n%2;
n = n/2;
a[i] = rem;
i++;
}

for (int j = i-1; j >=0; j--)
{
printf("%d", a[j]);
}

return 0;
}```Code language: C/AL (cal)```
• We have initialized an integer variable “n” which has value “4” this program will convert decimal number “4” to binary number.
• We have declared an integer variable “rem” which has stored the remainder
• We have declared an array “a” of the size [10] and initialized an integer variable “i” which has value “0”
• The “while” loop will run till the value of “n” gets “0”.
• In the “while” loop body the modulus “n%2” is taken and the value is stored in the variable “rem”; in the next step the value of “n” is divided by “2” and the value is stored in the variable “n”; in next step, the value of the variable “rem” is stored at the “i” index of an array and the value of the “i” is incremented.
• The main thing to note here is that the values of the remainder stored in the array are opposite that’s why we have to print them in reverse order for that we have used “for” loop.
• The “for” will start from the value “i-1” and runs till the values of “j” becomes greater than or equal to “0”; in the “for” loop body array is printed using “scanf” function.

## Output of C Program to Convert Decimal Number to Binary Number

```// As n=4
100```

100 is a binary of 4.