Skip to content

Instantly share code, notes, and snippets.

@felladrin
Created February 10, 2012 21:07
Show Gist options
  • Save felladrin/1792883 to your computer and use it in GitHub Desktop.
Save felladrin/1792883 to your computer and use it in GitHub Desktop.
Forma binária de um número inteiro
#include <iostream>
using namespace std;
void binario(int n)
{
int resto;
if(n <= 1)
{
cout << n;
return;
}
resto = n % 2;
binario(n >> 1);
cout << resto;
}
int main()
{
int n;
cout << "Digite um número inteiro não-negativo menor ou igual a 256: ";
cin >> n;
if (n < 0 || n > 256)
{
cout << "Você digitou um número negativo ou maior que 256! Game Over!";
return 0;
}
cout << "A forma binária desse número é ";
binario(n);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment