Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions convertb.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <stdio.h>

void decToBinary(int n) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hai caricato un file di troppo mi sà

if (n == 0) {
printf("0");
return;
}

int binary[32];
int i = 0;

while (n > 0) {
binary[i] = n % 2;
n = n / 2;
i++;
}

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

int main() {
int num;
printf("Insert first number: ");
scanf("%d", &num);

printf("The binary number is: ");
decToBinary(num);
printf("\n");

return 0;
}
32 changes: 32 additions & 0 deletions exercises/binary_converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,35 @@
Insert first number: 8
The binary number is: 1000
*/
#include <stdio.h>

void decToBinary(int n) {
if (n == 0) {
printf("0");
return;
}

int binary[32];
int i = 0;

while (n > 0) {
binary[i] = n % 2;
n = n / 2;
i++;
}

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

int main() {
int num;
printf("Insert first number: \n");
scanf("%d", &num);

printf("The binary number is: \n");
decToBinary(num);
printf("\n");

return 0;
}