Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <sstream>
- #include <string>
- using namespace std;
- string binary(short decimal)
- {
- int remainder;
- ostringstream output;
- if (decimal <= 1)
- {
- string s = to_string(decimal);
- return s;
- }
- remainder = decimal % 2;
- output << remainder;
- binary(decimal >> 1);
- return output.str();
- }
- int main()
- {
- short number;
- cout << "Please enter a positive integer.";
- cin >> number;
- if (number < 0)
- {
- cout << "You have entered a negative integer, please enter a positive integer";
- cin >> number;
- }
- else
- {
- cout << binary(number);
- }
- system("PAUSE");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement