mirror of
https://github.com/TheAlgorithms/C
synced 2024-11-21 21:11:57 +03:00
[feat/docs]: improve the binary to decimal algorithm (#1263)
This commit is contained in:
parent
6915d59738
commit
d222df7dc1
@ -1,37 +1,68 @@
|
||||
/**
|
||||
* Modified 24/05/2023, Indrranil Pawar
|
||||
* @brief Converts a number from [Binary to Decimal](https://en.wikipedia.org/wiki/Binary-coded_decimal).
|
||||
* @details
|
||||
*
|
||||
* C program that converts a binary number to its decimal equivalent.
|
||||
* Binary to decimal conversion is a process to convert a number
|
||||
* having a binary representation to its equivalent decimal representation.
|
||||
*
|
||||
* The base of both number systems is different.
|
||||
* Binary number system is base 2 number system while decimal number system is base 10 number system.
|
||||
* The numbers used in binary number system are 0 and 1 while decimal number system has numbers from 0 to 9.
|
||||
* The conversion of binary number to decimal number is done by multiplying
|
||||
* each digit of the binary number, starting from the rightmost digit, with the power of 2 and adding the result.
|
||||
*
|
||||
* @author [Anup Kumar Pawar](https://github.com/AnupKumarPanwar)
|
||||
* @author [David Leal](https://github.com/Panquesito7)
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdio.h> /// for IO operations
|
||||
#include <assert.h> /// for assert
|
||||
#include <math.h> /// for pow
|
||||
#include <inttypes.h> /// for uint64_t
|
||||
|
||||
/**
|
||||
* @brief Converts the given binary number
|
||||
* to its equivalent decimal number/value.
|
||||
* @param number The binary number to be converted
|
||||
* @returns The decimal equivalent of the binary number
|
||||
*/
|
||||
int convert_to_decimal(uint64_t number) {
|
||||
int decimal_number = 0, i = 0;
|
||||
|
||||
while (number > 0) {
|
||||
decimal_number += (number % 10) * pow(2, i);
|
||||
number = number / 10;
|
||||
i++;
|
||||
}
|
||||
|
||||
return decimal_number;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Self-test implementations
|
||||
* @returns void
|
||||
*/
|
||||
static void tests() {
|
||||
assert(convert_to_decimal(111) == 7);
|
||||
assert(convert_to_decimal(101) == 5);
|
||||
assert(convert_to_decimal(1010) == 10);
|
||||
assert(convert_to_decimal(1101) == 13);
|
||||
assert(convert_to_decimal(100001) == 33);
|
||||
assert(convert_to_decimal(10101001) == 169);
|
||||
assert(convert_to_decimal(111010) == 58);
|
||||
assert(convert_to_decimal(100000000) == 256);
|
||||
assert(convert_to_decimal(10000000000) == 1024);
|
||||
assert(convert_to_decimal(101110111) == 375);
|
||||
|
||||
printf("All tests have successfully passed!\n");
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Main function
|
||||
* @returns 0 on exit
|
||||
*/
|
||||
int main()
|
||||
{
|
||||
int binary_number, decimal_number = 0, temp = 1;
|
||||
|
||||
// Input the binary number
|
||||
printf("Enter any binary number: ");
|
||||
scanf("%d", &binary_number);
|
||||
|
||||
// Convert binary to decimal
|
||||
while (binary_number > 0)
|
||||
{
|
||||
// Extract the rightmost digit of the binary number
|
||||
int digit = binary_number % 10;
|
||||
|
||||
// Multiply the rightmost digit with the corresponding power of 2 and add to the decimal number
|
||||
decimal_number += digit * temp;
|
||||
|
||||
// Remove the rightmost digit from the binary number
|
||||
binary_number /= 10;
|
||||
|
||||
// Increase the power of 2 for the next digit
|
||||
temp *= 2;
|
||||
}
|
||||
|
||||
// Output the decimal equivalent
|
||||
printf("Decimal equivalent: %d\n", decimal_number);
|
||||
|
||||
tests(); // run self-test implementations
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user