Skip to content

feat: implementing binary_converter #308

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions exercises/binary_converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,29 @@
Insert first number: 8
The binary number is: 1000
*/
#include <iostream>
using namespace std;

// 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];
}

int main() {
int decimal;
cout << "Insert a decimal number: ";
cin >> decimal;
FromDecToBin(decimal);
return 0;
}