---
title: "Decimal to binary conversion"
subject: "Computer Programming"
chapter: 5
key: "semester_1/computer_programming:decimal-to-binary"
source: https://www.bctengineer.com/q/semester_1/computer_programming/decimal-to-binary
site: BCT Engineering
---

# Decimal to binary conversion

Computer Programming · Chapter 5: Control statements

- Asked in: 2074 Chaitra, 6 marks
- Video: [Convert Decimal Number To Binary Number | C Programming Example](https://www.youtube.com/watch?v=DSZ4pNCoYZY) — Portfolio Courses
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/decimal-to-binary
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-5

## Question

Write a program to convert a decimal number into binary number.

## Short answer

Divide the decimal number repeatedly by 2, storing remainders in an array; then print remainders in reverse order. For 0, output 0.

## Answer

Method: divide the number repeatedly by 2 and store the remainders; the binary number is the remainders read in reverse order (last remainder first).

Example, 45: 45/2 = 22 r 1; 22/2 = 11 r 0; 11/2 = 5 r 1; 5/2 = 2 r 1; 2/2 = 1 r 0; 1/2 = 0 r 1. Reading upwards gives 101101.

```c
#include <stdio.h>
int main(void)
{
    int n, i = 0, j, bin[32];
    printf("Enter a decimal number: ");
    scanf("%d", &n);
    if (n == 0)
        bin[i++] = 0;
    while (n > 0)
    {
        bin[i++] = n % 2;     /* remainder is the next bit (LSB first) */
        n = n / 2;
    }
    printf("Binary = ");
    for (j = i - 1; j >= 0; j--)
        printf("%d", bin[j]);
    printf("\n");
    return 0;
}
```
Output for 45:
```text
Enter a decimal number: Binary = 101101
```

The remainders are stored in the array `bin[]` and printed from the last index down to 0.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. How is binary found?
   A. Multiply by 2
   B. Divide by 2 repeatedly, read remainders in reverse
   C. Read remainders forward
   D. Divide by 10
   Answer: B. Last remainder first. (Key point: Method)

2. Why array bin[]?
   A. Store input
   B. Store quotient
   C. Remainders come LSB first, need reversing
   D. Count
   Answer: C. Printed from last index. (Key point: Array)

3. Binary of 45?
   A. 101011
   B. 110101
   C. 100111
   D. 101101
   Answer: D. Remainders 1,0,1,1,0,1 reversed. (Key point: 45)

4. Why handle n==0?
   A. Loop doesn't run, so store 0
   B. Avoid overflow
   C. Error
   D. Negative
   Answer: A. while(n>0) skips. (Key point: Zero)

## Related in Computer Programming

- [Binary to decimal in FORTRAN](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afortran-binary-to-decimal) — Computer Programming, chapter 11
- [Correct program: Celsius to Fahrenheit](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Acorrect-program-celsius-fahrenheit) — Computer Programming, chapter 3
- [Employee text file; sort into binary file](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-employee-sorted-binary) — Computer Programming, chapter 10
- [Binary file of 48 students; highest mark](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-binary-48-students-highest) — Computer Programming, chapter 10
- [Convert date from BS to AD](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Astruct-bs-to-ad) — Computer Programming, chapter 8, asked 2×
- [Convert case based on first character](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aswap-case-first-char-word) — Computer Programming, chapter 7

## Related in other subjects

- [Convert 37.432 decimal to binary](https://www.bctengineer.com/question.md?key=semester_3%2Fdigital_logic%3Aconvert-37-432-binary) — Digital Logic, chapter 1
- [Convert octal 623.77 to decimal, binary, hex](https://www.bctengineer.com/question.md?key=semester_3%2Fdigital_logic%3Aconvert-octal-623-77) — Digital Logic, chapter 1
- [8085 ALP: BCD to binary conversion](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8085-bcd-to-binary) — Microprocessor, chapter 2
- [Number conversions 33 to binary etc.](https://www.bctengineer.com/question.md?key=semester_2%2Fbasic_electronics_engineering%3Anumber-conversion-2067-chaitra) — Basic Electronics Engineering, chapter 6
