From 14d3d9ed41eb46cb808c4f9953208362ad9b622d Mon Sep 17 00:00:00 2001 From: linuchs Date: Sat, 21 Oct 2023 20:17:56 +0200 Subject: [PATCH 1/2] feat: implementing binary_converter --- exercises/binary_converter.cpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/exercises/binary_converter.cpp b/exercises/binary_converter.cpp index 19bf37d..fc7b360 100644 --- a/exercises/binary_converter.cpp +++ b/exercises/binary_converter.cpp @@ -5,3 +5,26 @@ Insert first number: 8 The binary number is: 1000 */ + +#include +using namespace std; + +int main() +{ + int first; + int binary[16]; + + cout << "Insert the first number: "; + cin >> first; + int i = 0; + while (first > 0) { + binary[i] = first % 2; + first = first / 2; + i++; + } + + cout <<"The binary number is: "; + for (int j = i - 1; j >= 0; j--) + cout << binary[j]; + return 0; +} From ab3e88e35ea4227c95da98c622d7ca04976e606a Mon Sep 17 00:00:00 2001 From: linuchs Date: Mon, 23 Oct 2023 14:25:26 +0200 Subject: [PATCH 2/2] feat: Massive improvement --- exercises/binary_converter.cpp | 45 ++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/exercises/binary_converter.cpp b/exercises/binary_converter.cpp index fc7b360..505943e 100644 --- a/exercises/binary_converter.cpp +++ b/exercises/binary_converter.cpp @@ -5,26 +5,29 @@ Insert first number: 8 The binary number is: 1000 */ +#include +using namespace std; -#include -using namespace std; - -int main() -{ - int first; - int binary[16]; +// here we convert decimal to binary +void FromDecToBin(int number) { + // array to fill with binary number + int binary[16]; + int i = 0; + while (number > 0) { + // now fill the binary array with the rest + binary[i] = number % 2; + number = number / 2; + i++; + } + // at the end we print the binary array + for (int j = i - 1; j >= 0; j--) + cout << binary[j]; +} - cout << "Insert the first number: "; - cin >> first; - int i = 0; - while (first > 0) { - binary[i] = first % 2; - first = first / 2; - i++; - } - - cout <<"The binary number is: "; - for (int j = i - 1; j >= 0; j--) - cout << binary[j]; - return 0; -} +int main() { + int decimal; + cout << "Insert a decimal number: "; + cin >> decimal; + FromDecToBin(decimal); + return 0; +}