-
Notifications
You must be signed in to change notification settings - Fork 0
/
Decimal_to_Binary.c
51 lines (44 loc) · 1.14 KB
/
Decimal_to_Binary.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
46
47
48
49
50
51
#include "base.h"
/**
* dec_to_bin - This function converts decimal to binary
* @n: user input
* Return: returns 0 on success, 1 if otherwise
*/
int dec_to_bin(unsigned int n)
{
// declare a character array to hold the binary digits
char binary[250];
int i; // will be used for indexing
i = 0;
while (n >= 1) // The loop breaks once n is less than 1
{
if ((n % 2) == 0)
{
binary[i] = '0'; // if remainder is 0 add (0) to binary at postion i
}
else
{
binary[i] = '1'; // else add 1
}
i++; // i is incremented
n /= 2; // n is reduced by diving with 2
}
binary[i] = '\0';
/**
* The binary numbers need to be swapped to start from MSB(bottom) to the LSB(top)
*/
char temp;
int len;
int j;
len = strlen(binary); // lenght of binary
j = 0;
while (j < len / 2) // loops through half of binary
{
temp = binary[j];
binary[j] = binary[len - j - 1];
binary[len - j - 1] = temp;
j++;
}
printf("🔢 Your Binary Conversion Result is: %-5s\n", binary);
return (0);
}