-
Notifications
You must be signed in to change notification settings - Fork 8
/
DecimalToBinary.c
45 lines (37 loc) · 930 Bytes
/
DecimalToBinary.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Convert base10(decimal) values to base2(binary)
#include <stdio.h>
void PrintBits(int power, int num){
// firstOne is a flag that stops "0" being printed before a "1" is printed
int firstOne = 0;
// Conversion loop
while (power > 0) {
if (num >= power) {
firstOne = 1;
printf("1");
num %= power;
} else if (firstOne == 1) {
printf("0");
}
power /= 2;
}
printf("\n");
}
int main() {
int num = 0;
int power = 1;
// Getting input
printf("Please enter a numeric value: ");
scanf("%d", & num);
printf("%d represented in binary is: ", num);
// Determine highest order of magnitude needed for the base2 conversion
while (power < num) {
power *= 2;
}
// Edge case in event of 0 being entered
if (num == 0) {
printf("0\n");
} else {
PrintBits(power, num);
}
return (0);
}