|
1 | 1 | /**
|
2 |
| -* Modified 24/05/2023, Indrranil Pawar |
3 |
| -* |
4 |
| -* C program that converts a binary number to its decimal equivalent. |
| 2 | + * @brief Converts a number from [Binary to Decimal](https://en.wikipedia.org/wiki/Binary-coded_decimal). |
| 3 | + * @details |
| 4 | + * |
| 5 | + * Binary to decimal conversion is a process to convert a number |
| 6 | + * having a binary representation to its equivalent decimal representation. |
| 7 | + * |
| 8 | + * The base of both number systems is different. |
| 9 | + * Binary number system is base 2 number system while decimal number system is base 10 number system. |
| 10 | + * The numbers used in binary number system are 0 and 1 while decimal number system has numbers from 0 to 9. |
| 11 | + * The conversion of binary number to decimal number is done by multiplying |
| 12 | + * each digit of the binary number, starting from the rightmost digit, with the power of 2 and adding the result. |
| 13 | + * |
| 14 | + * @author [Anup Kumar Pawar](https://github.com/AnupKumarPanwar) |
| 15 | + * @author [David Leal](https://github.com/Panquesito7) |
5 | 16 | */
|
6 | 17 |
|
7 |
| -#include <stdio.h> |
| 18 | +#include <stdio.h> /// for IO operations |
| 19 | +#include <assert.h> /// for assert |
| 20 | +#include <math.h> /// for pow |
| 21 | +#include <inttypes.h> /// for uint64_t |
8 | 22 |
|
9 |
| -int main() |
10 |
| -{ |
11 |
| - int binary_number, decimal_number = 0, temp = 1; |
12 |
| - |
13 |
| - // Input the binary number |
14 |
| - printf("Enter any binary number: "); |
15 |
| - scanf("%d", &binary_number); |
16 |
| - |
17 |
| - // Convert binary to decimal |
18 |
| - while (binary_number > 0) |
19 |
| - { |
20 |
| - // Extract the rightmost digit of the binary number |
21 |
| - int digit = binary_number % 10; |
| 23 | +/** |
| 24 | + * @brief Converts the given binary number |
| 25 | + * to its equivalent decimal number/value. |
| 26 | + * @param number The binary number to be converted |
| 27 | + * @returns The decimal equivalent of the binary number |
| 28 | +*/ |
| 29 | +int convert_to_decimal(uint64_t number) { |
| 30 | + int decimal_number = 0, i = 0; |
22 | 31 |
|
23 |
| - // Multiply the rightmost digit with the corresponding power of 2 and add to the decimal number |
24 |
| - decimal_number += digit * temp; |
| 32 | + while (number > 0) { |
| 33 | + decimal_number += (number % 10) * pow(2, i); |
| 34 | + number = number / 10; |
| 35 | + i++; |
| 36 | + } |
25 | 37 |
|
26 |
| - // Remove the rightmost digit from the binary number |
27 |
| - binary_number /= 10; |
| 38 | + return decimal_number; |
| 39 | +} |
28 | 40 |
|
29 |
| - // Increase the power of 2 for the next digit |
30 |
| - temp *= 2; |
31 |
| - } |
| 41 | +/** |
| 42 | + * @brief Self-test implementations |
| 43 | + * @returns void |
| 44 | +*/ |
| 45 | +static void tests() { |
| 46 | + assert(convert_to_decimal(111) == 7); |
| 47 | + assert(convert_to_decimal(101) == 5); |
| 48 | + assert(convert_to_decimal(1010) == 10); |
| 49 | + assert(convert_to_decimal(1101) == 13); |
| 50 | + assert(convert_to_decimal(100001) == 33); |
| 51 | + assert(convert_to_decimal(10101001) == 169); |
| 52 | + assert(convert_to_decimal(111010) == 58); |
| 53 | + assert(convert_to_decimal(100000000) == 256); |
| 54 | + assert(convert_to_decimal(10000000000) == 1024); |
| 55 | + assert(convert_to_decimal(101110111) == 375); |
32 | 56 |
|
33 |
| - // Output the decimal equivalent |
34 |
| - printf("Decimal equivalent: %d\n", decimal_number); |
| 57 | + printf("All tests have successfully passed!\n"); |
| 58 | +} |
35 | 59 |
|
| 60 | +/** |
| 61 | + * @brief Main function |
| 62 | + * @returns 0 on exit |
| 63 | +*/ |
| 64 | +int main() |
| 65 | +{ |
| 66 | + tests(); // run self-test implementations |
36 | 67 | return 0;
|
37 | 68 | }
|
0 commit comments