forked from portfoliocourses/c-example-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decimal_to_binary.c
103 lines (85 loc) · 2.92 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*******************************************************************************
*
* Program: Convert Decimal To Binary
*
* Description: Convert a non-negative integer decimal number to a binary number
* stored in a string using C.
*
* YouTube Lesson: https://www.youtube.com/watch?v=DSZ4pNCoYZY
*
* Author: Kevin Browne @ https://portfoliocourses.com
*
*******************************************************************************/
#include <stdio.h>
int main()
{
// used to store binary number represented as a string
char binary[256];
// used to store the decimal number
int decimal;
do
{
// prompt the user for the number, store it into decimal
printf("Enter A Non-Negative Integer: ");
scanf("%d", &decimal);
// if the number is negative, tell the user it must be non-negative
if (decimal < 0)
printf("Integer Must Be Non-Negative");
// continue to ask the user until a non-negative number is entered
} while (decimal < 0);
// length keeps track of the current length of binary number as we build it
int length = 0;
// perform the conversion algorithm (see description below main function)
do
{
// determine the next digit of the binary number
if (decimal % 2 == 0) binary[length] = '0';
else binary[length] = '1';
// divide the number by 2 for the next iteration of the algorithm
decimal /= 2;
// advance length by 1 to set the next index of binary, the next digit in
// our binary number
length++;
// continue with the algorithm until decimal is equal to 0
} while (decimal != 0);
// put a null terminator at the end of the string to terminate the string
binary[length] = '\0';
// the binary number stored in binary is technically reversed, so we reverse
// the string to put it in the correct order
// find the middle position of the string
int middle = length / 2;
// swap each character/digit at the beginning of the string until the middle
// of the string with the corresponding character at the end of the string in
// order to reverse the string, see...
// - Reverse A String: https://www.youtube.com/watch?v=PtSHcou0WIs
char temp;
for (int i = 0; i < middle; i++)
{
temp = binary[i];
binary[i] = binary[length - i - 1];
binary[length - i - 1] = temp;
}
// output the binary number
printf("\nBinary Number: %s\n\n", binary);
return 0;
}
// Conversion Algorithm:
//
// Repeat until the number is equal to 0:
//
// Divide the number by 2:
// - remainder is the next digit in the binary number
// - quotient is used as the number in next iteration
//
// So for exmample if we wish to conver the number 43:
//
// 43 / 2 = 21 1 remainder
// 21 / 2 = 10 1 remainder
// 10 / 2 = 5 0 remainder
// 5 / 2 = 2 1 remainder
// 2 / 2 = 1 0 remainder
// 1 / 2 = 0 1 remainder
//
// Binary Number: 101011
// / | | \
// 32 8 2 1 = 43