Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- char int_to_char (int n) {
- if (n < 10) return '0' + n;
- else return 'A' + n - 10;
- }
- string change_base (int n, int b) {
- string s;
- if (n < b) {
- s = int_to_char(n);
- return s;
- }
- else {
- s = int_to_char(n%b);
- return change_base(n/b, b) + s;
- }
- }
- int main() {
- int n;
- while (cin >> n) {
- cout << n << " = " << change_base(n, 2) << ", " << change_base(n, 8) << ", " << change_base(n, 16) << endl;
- }
- }
Add Comment
Please, Sign In to add comment