Skip to content

Commit 7990bea

Browse files
committed
ho scritto il codice
1 parent 4499583 commit 7990bea

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

exercises/binary_converter.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,35 @@
55
Insert first number: 8
66
The binary number is: 1000
77
*/
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

Comments
 (0)