📘 Lesson · Lesson 66
Decimal to Binary
Decimal to Binary
Decimal to Binary
💡 Note
To convert a decimal number to binary, repeatedly divide by 2 and collect the remainders in reverse.
Program
C
#include <stdio.h>
int main() {
int n = 13, binary[32], i = 0;
while (n > 0) {
binary[i++] = n % 2;
n /= 2;
}
for (int j = i - 1; j >= 0; j--)
printf("%d", binary[j]); // 1101
return 0;
}Output:
1101
1101
Summary
- Divide by 2, store remainders, then print them in reverse.
- 13 in binary is 1101.
Decimal से Binary
💡 Note
Decimal को binary में बदलने को बार-बार 2 से divide करें और remainders उल्टे क्रम में इकट्ठा करें।
Program
C
#include <stdio.h>
int main() {
int n = 13, binary[32], i = 0;
while (n > 0) {
binary[i++] = n % 2;
n /= 2;
}
for (int j = i - 1; j >= 0; j--)
printf("%d", binary[j]); // 1101
return 0;
}Output:
1101
1101
सारांश
- 2 से divide करें, remainders store करें, फिर उल्टा print करें।
- 13 का binary 1101 है।