From ec994a46df4a88f6e7b592827fe89b2f2a7ed221 Mon Sep 17 00:00:00 2001 From: Hyder Date: Thu, 14 Aug 2025 20:15:29 +0300 Subject: Solved the binary to decimal converter exercise --- BTD/28bitToDecimal.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 BTD/28bitToDecimal.c (limited to 'BTD') diff --git a/BTD/28bitToDecimal.c b/BTD/28bitToDecimal.c new file mode 100644 index 0000000..ab60959 --- /dev/null +++ b/BTD/28bitToDecimal.c @@ -0,0 +1,31 @@ +# include + +# define bitvalue_length 27 + +unsigned long int powerOFtwo(int value) { + unsigned long int result = 1; + for (int i = value;i > 0;i--) { + result *= 2; + } + return result; +} + +int main () { + printf("28-bit binary number converter to decimal:\n\n"); + char bitvalue[28]; + scanf("%s", bitvalue); + unsigned long int decimalValue = 0; + for (int i = bitvalue_length;i >= 0;i--) { + if (bitvalue[i] == '0') { + decimalValue += 0; + } + else if (bitvalue[i] == '1') { + unsigned long int tmp = powerOFtwo(bitvalue_length - i); + decimalValue += tmp; + } + } + + printf("%lu\n" , decimalValue); + + return 0; +} \ No newline at end of file -- cgit v1.2.3