Skip to content

Instantly share code, notes, and snippets.

@misterpoloy
Created January 10, 2020 20:05
Show Gist options
  • Save misterpoloy/f50eac2e39252039fb89764c0ccb37a4 to your computer and use it in GitHub Desktop.
Save misterpoloy/f50eac2e39252039fb89764c0ccb37a4 to your computer and use it in GitHub Desktop.
Decimal to binary
#include <iostream>
#include <list>
std::list <int> decimalToBinary(int n) {
std::list <int> binary;
int remainder;
int number = n;
while (number > 0) {
int quotient = number / 2;
remainder = number % 2;
number = quotient;
binary.push_front(remainder);
};
return binary;
};
int main() {
std::cout << "Ingresa el número:" << std::endl;
int n;
std::cin >> n;
std::list <int> decimal = decimalToBinary(n);
for (int x : decimal)
std::cout << x;
std::cout << " " << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment