Created
February 10, 2012 21:07
-
-
Save felladrin/1792883 to your computer and use it in GitHub Desktop.
Forma binária de um número inteiro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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