Skip to content

Commit 63b848e

Browse files
committed
Print the binary representation of a given integer. closes rathoresrikant#457
1 parent 2bf5215 commit 63b848e

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

IntToBin.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <stdlib.h>
2+
#include <stdio.h>
3+
4+
void IntToBin(int num) {
5+
if (num > 1 ) {
6+
IntToBin(num>>1);
7+
}
8+
printf("%d", num%2);
9+
return;
10+
}
11+
12+
int main(int argc, char *argv[ ]) {
13+
int integer;
14+
15+
if (argc == 1) {
16+
printf("Type an Integer:\n");
17+
scanf("%d", &integer);
18+
} else if (argc == 2) {
19+
integer = atoi(argv[argc-1]);
20+
} else {
21+
printf("Usage: %s [INTEGER]\n", argv[0]);
22+
return -1;
23+
}
24+
if (integer >= 0) {
25+
IntToBin(integer);
26+
printf("\n");
27+
} else {
28+
printf("Invalid input.\n");
29+
}
30+
return 0;
31+
}

0 commit comments

Comments
 (0)