We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 4499583 commit 7990beaCopy full SHA for 7990bea
exercises/binary_converter.cpp
@@ -5,3 +5,35 @@
5
Insert first number: 8
6
The binary number is: 1000
7
*/
8
+#include <stdio.h>
9
+
10
+void decToBinary(int n) {
11
+ if (n == 0) {
12
+ printf("0");
13
+ return;
14
+ }
15
16
+ int binary[32];
17
+ int i = 0;
18
19
+ while (n > 0) {
20
+ binary[i] = n % 2;
21
+ n = n / 2;
22
+ i++;
23
24
25
+ for (int j = i - 1; j >= 0; j--)
26
+ printf("%d", binary[j]);
27
+}
28
29
+int main() {
30
+ int num;
31
+ printf("Insert first number: ");
32
+ scanf("%d", &num);
33
34
+ printf("The binary number is: ");
35
+ decToBinary(num);
36
+ printf("\n");
37
38
+ return 0;
39
0 commit comments